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
6
votes
1 answer
Static initializer error if placed before the declaration
I noticed something in static initializers which may be a bug in the javac. I have constructed a scenario where I can assign a variable a value but not read that value back.
The two examples are below, the first compiles fine, the second gets an…
user545199
5
votes
6 answers
Can I guarantee the order in which static initializers are run in Java?
I have a Set class (This is J2ME, so I have limited access to the standard API; just to explain my apparent wheel-reinvention). I am using my set class to create constant sets of things in classes and subclasses. It sort of looks like this...
class…

izb
- 50,101
- 39
- 117
- 168
5
votes
3 answers
Using a macro to initialize a big array of non-Copy elements
I'm trying to initialize a big array of elements with the same initializer. 64 elements is just an example — I want to make it at least 16k. Unfortunately a simple
let array : [AllocatedMemory; 64] = [AllocatedMemory::{mem:&mut…

hellcatv
- 573
- 4
- 21
5
votes
3 answers
Java - Class type from inside static initialization block
Is it possible to get the class type from inside the static initialization block?
This is a simplified version of what I currently have::
class Person extends SuperClass {
String firstName;
static{
// This function is on the…

Chris Dutrow
- 48,402
- 65
- 188
- 258
4
votes
3 answers
Unable to read input from static block using scanner class
i want to read the data from the user in the static block and need to check some condition there but when i am trying to call nextInt() it causes some error
public class Test {
static int B,H;
static{
Scanner s=new…

Promod Kumar
- 121
- 1
- 8
4
votes
3 answers
Static initialization of a struct with class members
I have a struct that's defined with a large number of vanilla char* pointers, but also an object member. When I try to statically initialize such a struct, I get a compiler error.
typedef struct
{
const char* pszA;
// ... snip ...
const…

JSBձոգչ
- 40,684
- 18
- 101
- 169
4
votes
2 answers
What's the best way to test a class static initializer?
I have a class which has fairly complex static initialization. I'm reading files from a directory, then parsing those json files, mapping to objects, and filling up a list. You can imagine, there can rise some exceptions, and I need to cover, and…

JSONStatham
- 353
- 4
- 18
4
votes
0 answers
Static initializers and final constants in Java
Given the following simple code in Java.
final class Demo
{
public static final long serialVersionUID=1L;
static
{
System.out.println("Static constructor invoked.");
}
}
public final class Main
{
public static void…

Tiny
- 27,221
- 105
- 339
- 599
3
votes
3 answers
Collection Initializers in C#
In Java, I can create an List and immediately populate it using a static initializer. Something like this:
List <String> list = new ArrayList<String>()
{{
Add("a");
Add("b");
Add("c");
}}
Which is convenient, because I can…

user489041
- 27,916
- 55
- 135
- 204
3
votes
1 answer
Request for the detailed description of "Static Initialization Order Fiasco"
I read about the SIOF in the faq-lite and still I really don't understand why the issue happens. I have a static library(.a) and I use that library to use its static const data member object type. Then that static const data member object type I use…

domlao
- 15,663
- 34
- 95
- 134
3
votes
3 answers
Static initializer cannot reference a field before it is defined
I have the following code with the error commented
public final class MyStaticClass {
private MyStaticClass() {}
static {
a = new A();
b = new B(a); // Cannot access a field before it is defined
}
private…

Andy
- 3,228
- 8
- 40
- 65
3
votes
5 answers
How should I make up for the lack of static initializers in PHP?
I'm thinking about putting every class into a separate file and doing the static initialization outside of the class definition.
The problem with this is the fact that the initialization will happen before the said class is actually needed (it will…

Attila Kun
- 2,215
- 2
- 23
- 33
3
votes
2 answers
Java: ExceptionInInitializerError caused by NullPointerException when constructing a Locale object
I'm working on localization for a program I've written with a couple other guys. Most of the strings now load in the appropriate language from an ini file. I'm trying to do the same with the format of currency in the program. However, I'm getting a…

electrickoolaid42
- 87
- 1
- 9
2
votes
2 answers
Global initialization in Android
I'm writing some library code distributed as a jar file that developers will need to initialize with an application id before using. Initialization is just a function call, like
MyLibrary.initialize("16ea53b");
The tricky thing is that I am not…

lacker
- 5,470
- 6
- 36
- 38
2
votes
3 answers
Do the JDK libraries provide a lambda 'invoker' utility class?
I'm looking to replace the need for separate stand-alone static initialer functions with lambdas. e.g. I'd like to replace something like this...
class Foo {
private static final Set keywords = keywords();
private static Set…

BillRobertson42
- 12,602
- 4
- 40
- 57