Static block is not executed without initialization

I have a question regarding static blocks:

Let's say I have a class similar to this:

class SomeClass { static { System.out.println("static block"); } } 

and somewhere I define a variable of type SomeClass .

 public static void main(String args[]) { SomeClass foo; } 

Now I thought that the static block would be executed, but it is not. As far as I know, a static block is executed as soon as the class loader loads the SomeClass class. Now to my real question:

Is the class loading as soon as I define a variable of this type? . If so, why is the static block not executing?

If the answer should be negative, then how can I find out if the class has already been loaded by the class loader and what are the different options for loading the class (I know that 2: initializing the variable and using a static field / method)

+7
java static-block
source share
2 answers

Refer to this document: http://www.javaworld.com/article/2077260/learn-java/learn-java-the-basics-of-java-class-loaders.html

So when do classes load? There are only two cases: when a new bytecode is executed (for example, FooClass f = new FooClass ();) and when bytecodes make a static reference to the class (for example, System.out).

In your example, SomeClass foo; does not execute SomeClass bytecode and does not statistically reference SomeClass. This is why the class is not loaded.

So, following your example, add a static field to the class

 public class SomeClass { static { System.out.println("static block"); } static String abc = "abc"; } 

SomeClass is loaded either in:

 SomeClass foo = new SomeClass(); 

or

 System.out.println(SomeClass.abc); 
+5
source share

Is the class loaded as soon as I define a variable of this type ?.

Yes, it loads 1 but it will not be initialized as a result of the variable declaration. However, when you create an instance of this type or access a static field of this type, it is enough to start initialization, including the execution of static blocks.

See this related Q & A - When does a static class initialize? - which lists all the things that can initiate initialization.


How to find out if a class has already been loaded by the class loader and what are the different options for loading the class (I know that 2: initializing a variable and using a static field / method)

The only ways I can come up with to determine when the class is loaded (as opposed to initialization) are:

  • Enabling JVM class loader messages (using -verbose:class ) or

  • using the class loader class, which notices and does something suitable when it sees a request to load the class.

Actually the class will be loaded:

  • when it is explicitly loaded using Class.forName or a similar or direct call to the class loader,

  • when you need to load it to link another class, or

  • when starting the JVM, if the class is called an entry point class.

The steps for loading / linking / initializing are listed in Chapter 12 of the JLS .


1 - Actually, SomeClass needs to be loaded at the same time that the class containing this main method is connected; that is, before the method containing this local declaration is called.

+4
source share

All Articles