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
379
votes
9 answers
What is the difference between a static and a non-static initialization code block
My question is about one particular usage of static keyword. It is possible to use static keyword to cover a code block within a class which does not belong to any function. For example following code compiles:
public class Test {
private static…

Szere Dyeri
- 14,916
- 11
- 39
- 42
144
votes
6 answers
Are Java static initializers thread safe?
I'm using a static code block to initialize some controllers in a registry I have. My question is therefore, can I guarantee that this static code block will only absolutely be called once when the class is first loaded? I understand I cannot…

simon622
- 3,248
- 3
- 19
- 12
144
votes
8 answers
Why doesn't Java allow to throw a checked exception from static initialization block?
Why doesn't Java allow to throw a checked exception from a static initialization block? What was the reason behind this design decision?

missingfaktor
- 90,905
- 62
- 285
- 365
113
votes
10 answers
Use of Initializers vs Constructors in Java
So I've been brushing up on my Java skills as of late and have found a few bits of functionality that I didn't know about previously. Static and Instance Initializers are two such techniques.
My question is when would one use an initializer instead…

Inertiatic
- 1,223
- 2
- 9
- 8
105
votes
8 answers
In what order do static/instance initializer blocks in Java run?
Say a project contains several classes, each of which has a static initializer block. In what order do those blocks run? I know that within a class, such blocks are run in the order they appear in the code. I've read that it's the same across…

Pops
- 30,199
- 37
- 136
- 151
79
votes
8 answers
Java enum reverse look-up best practice
I saw it suggested on a blog that the following was a reasonable way to do a "reverse-lookup" using the getCode(int) in a Java enum:
public enum Status {
WAITING(0),
READY(1),
SKIPPED(-1),
COMPLETED(5);
private static final…

Armand
- 23,463
- 20
- 90
- 119
59
votes
3 answers
Initialization Order of Static Fields in Static Class
given the following code:
public static class Helpers
{
private static Char[] myChars = new Char[] {'a', 'b'};
private static Int32 myCharsSize = myChars.Length;
}
Is it guaranteed that myChars will be initialized before I use its length to…

Alex
- 75,813
- 86
- 255
- 348
49
votes
6 answers
How to force a class to be initialised?
What is the best and cleanest way to do this? Specifically, I need some code in a static initializer block to run in that class, but I'd like to make this as clean-looking as possible.

Artem
- 6,420
- 6
- 26
- 26
21
votes
4 answers
Program hangs if thread is created in static initializer block
I have come across a situation where my program hangs, looks like deadlock. But I tried figuring it out with jconsole and visualvm, but they didn't detect any deadlock. Sample code:
public class StaticInitializer {
private static int state =…

thomas
- 310
- 2
- 12
14
votes
2 answers
Kotlin - Is it possible to initialize companion object before the init block in a class?
Is it possible to initialize an companion object before the init block in a Kotlin class? If so, how? If not, is there a way to accomplish the same thing.
I have the following scenario,
class A(val iname: String) {
init {
foo.add(this)
}
…

Filip Allberg
- 3,941
- 3
- 20
- 37
12
votes
2 answers
How do you disable lazy class loading/initialization in Sun's JVM?
By default, Sun's JVM both lazily loads classes and lazily initializes (i.e. calls their methods) them. Consider the following class, ClinitBomb, which throws an Exception during a static{} block.
public class ClinitBomb {
static {
…

jade
- 744
- 5
- 16
11
votes
6 answers
.crt section? What does this warning mean?
I've got this warning recently (VC++ 2010)
warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
I'm assuming this is the Critical Section. It's been a while since my Operating Systems course, so I can't…

MGZero
- 5,812
- 5
- 29
- 46
11
votes
7 answers
How to check whether a class is initialized?
You'll probably ask, why would I want to do that - it's because I'm using a class (from an external library) which does stuff in its static initializer and I need to know whether it's been done or not.
I looked at ClassLoader, but didn't find…

mik01aj
- 11,928
- 15
- 76
- 119
11
votes
6 answers
how to register a java class if the static initializer isn't called till the class is referenced
I've an interface implemented by classes that perform a file processing, say searching or whatever.
public interface FileProcessorInterface {
public void processFile(String fileName);
}
Then i have a different implementation for each file…

AgostinoX
- 7,477
- 20
- 77
- 137
10
votes
4 answers
How can I add javadoc to a static initializer in Java?
I have refactored a class and moved some code from the constructor to a static initializer. What should I do with the javadoc that was on the constructor? Is it possible to add javadoc to a static initializer?

tttppp
- 7,723
- 8
- 32
- 38