The static initializer is a static {} block of code inside java class, and run only one time before the constructor or main method is called.
Questions tagged [static-initializer]
89 questions
2
votes
3 answers
Static initializing a struct of unions of arrays
I am trying to write static initializers for this class:
class Cube3x3
{
union CornerData
{
u8 mData8[8];
u32 mData16[4];
u32 mData32[2];
u64 mData64;
};
union EdgeData
{
u8 mData8[12];
…

VoidStar
- 5,241
- 1
- 31
- 45
2
votes
6 answers
What benefits does main(...) provide over using a static-initializer as a pseudo entry-point?
The entry point into a program in java is typically something like this
// MyMain.java
public class MyMain{
//whatever
public static void main(String args[]){
System.out.println("balderdash");
}
…

Everyone
- 2,366
- 2
- 26
- 39
1
vote
1 answer
Finding all dynamic initializations in a library
I have several large code bases which compile into dynamic libraries. I know that some of these have some very expensive dynamic global dynamic initializers. (That is, global instances of classes/structs which are very expensive to construct.)
I…

SoapBox
- 20,457
- 3
- 51
- 87
1
vote
1 answer
Inherit execution of static initializer blocks in JavaScript
I have code like:
class A {
static {
console.log("A");
}
}
class B extends A {
static {
console.log("B");
}
}
Why doesn't it print A twice? I would like a way in JavaScript to execute code of a class whenever it is…

kungfooman
- 4,473
- 1
- 44
- 33
1
vote
1 answer
Static initializing block is not working as expected
I have two classes, "Test1" and "Test0", as shown in this code.
public class Test1 {
public static void main(String...args) {
System.out.print(Test0.randomName);
}
}
public class Test0 {
public static String randomName =…

Zwe Myint Mo
- 25
- 5
1
vote
0 answers
Run Initialization Code when a Script is Loaded
I have a GDScript file, and I would like to be able to run a block of code whenever the script is loaded. I know _init will run whenever an instance is constructed and _ready will run when it's added to the scene tree. I want to run code before…

Silvio Mayolo
- 62,821
- 6
- 74
- 116
1
vote
3 answers
Default Constructor is getting executed before Static block in java
When we load a class in java, first static block get executed and then default constructor. but in below peace of code, What I observed that default constructor is getting executed before static block.
public class Hello {
private static…

Kms
- 1,082
- 2
- 11
- 27
1
vote
0 answers
Detailed Initialization Procedure class variable initializer
I saw many confusing answers about the following point :
Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single…

Mohammad Karmi
- 1,403
- 3
- 26
- 42
1
vote
1 answer
Are static initialisers necessarily executed in hierarchical order?
Let's say I have a class
class A{
public final static TreeMap tmap = new TreeMap<>();
int x;
static{
tmap.put("x:I", "Hello");
}
}
and I create a subclass
class B extends A{
long y;
static{
…

User1291
- 7,664
- 8
- 51
- 108
1
vote
1 answer
Clearing activity from history unloads application
I've been playing arround with using the camera flashlight from many interfaces, Activities, Widgets and Notifications, and to coordinate all interfaces I'm using a static initialization block in a random class to instance all needed components,…

Juan
- 124
- 2
- 8
1
vote
2 answers
Why static initializer allow re-initialization of static variable in Java?
I am studying static initializers in Java. I came through a source code as given below:
public class A {
private static int count = 5;
final static int STEP = 10;
boolean alive;
static {
count = 1;
}
…

patrick.1729
- 4,222
- 2
- 20
- 29
1
vote
0 answers
Is there a way to customize the default module initializer?
I am compiling an existing C++ library as a C++/CLI DLL. The C++ library has two initialization routines which need to be called to set up various lookup tables.
The C++ library originally used std::once_flag and std::call_once() from to…

Daniel Trebbien
- 38,421
- 18
- 121
- 193
1
vote
1 answer
WebAPI 2 JSON serialization of object-arrays with nested static object instances fails
I needed to have a Web API service with some fake data for a new PoC. The objectmodel for this is quite simple, 2 classes, where class1 has a nested array of class2.
So I started building a FakeDataService where I just hardcoded some data.
For…

hcd
- 1,338
- 2
- 10
- 15
1
vote
1 answer
Java ClassLoader Issue or Concurrency Error?
After a WebLogic app has been running fine for a few weeks I suddenly get an Exception:
<[ServletContext@60724164[app:whatever3000 module:whatever3000.war path:
spec-version:2.5]]…

squarewav
- 383
- 2
- 8
1
vote
1 answer
Strange exception while using linq2sql
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException:…

zerkms
- 249,484
- 69
- 436
- 539