Program hangs if thread is created in static initializer block

You’re not just starting another thread – you’re joining on it. That new thread has to wait for StaticInitializer to be fully initialized before it can proceed, because it’s trying to set the state field… and initialization is already in progress, so it waits. However, it’s going to be waiting forever, because that initialization is … Read more

Why doesn’t Java allow to throw a checked exception from static initialization block?

Because it is not possible to handle these checked exceptions in your source. You do not have any control over the initialization process and static{} blocks cannot be called from your source so that you could surround them with try-catch. Because you cannot handle any error indicated by a checked exception, it was decided to … Read more

What is the difference between a static and a non-static initialization code block

The code block with the static modifier signifies a class initializer; without the static modifier the code block is an instance initializer. Class initializers are executed in the order they are defined (top down, just like simple variable initializers) when the class is loaded (actually, when it’s resolved, but that’s a technicality). Instance initializers are … Read more