What is a static block in Java
When is the static block of a class executed?
I have 2 glasses, let's call them a.jar and b.jar.
b.jar depends on a.jar.
In a.jar I have defined a class, let's call it StaticClass. In the StaticClass I have defined a static block and called a method called "init":
in b.jar I have a main, so in main I expect the init () method to be called, but actually not. I suspect this is because the StaticClass was not loaded by the JVM someone might tell me
- Is my conclusion correct?
- What causes the JVM to load a class?
- How can I have the static block run automatically?
Yes you are right. Static initialization blocks are executed when the JVM (Class Loader - to be precise) is loaded (which is the case when it is first referenced in the code).
You can force this method to be called by calling it explicitly. This is preferable to using the JVM.
You can also try forcing the JVM to load the class and call its static blocks.
Yes, you are right, since you are not using yours, it will not be loaded by the VM and therefore never run.
Your second question will likely have to go the hard way and scan and load all the classes available.
First, class loading is different from class initialization. For anyone looking for an explanation from the Java language specification of when a static block will run - here it is.
The JLS §8.7 says that:
A static initializer declared in a class is executed when the class is initialized (§12.4.2).
What does the initialization mean? We refer to JLS §12.4.2. This describes a detailed initialization procedure. Point JLS §12.4.1 could however be more appropriate here. It says that:
A class or an interface type T is initialized immediately before the first occurrence of one of the following actions:
T is a class and an instance of T is created. T is a class and a static method declared by T is called. A static field declared by T is assigned. A static field declared by T is used and the field is not a constant variable (§4.12.4). T is a top-level class (§7.6), and an assert statement (§14.10) lexically nested in T (§8.1.3) is executed.
For the static initialization block to run automatically, you must force one of these options.
You are right, the easiest way is to access the class, for example a
Or something like that in your main method. This ensures that the class is loaded by the class loader.
The static code will be executed when your class is referenced (I believe).
Hence, it should be run when you have a new instance of create or call one of the static methods .
The static block is executed when a loaded class is first initialized or referenced. Loading a class does not mean that the class should be initialized. Loading the JVM class is a separate problem.
Yes, the static initializer will run when the class is loaded. This usually occurs the first time you access the class in the class loading context.
This StaticClass should be extended in the main method class b.jar, then the static block and init () are called automatically
The static block is executed when the JVM load class is used.
In your example here, you can call the method of by calling the class
like StaticClass staticClass = new StaticClass ();
that is more preferred
- Are programming languages tested? If so, how
- Robots will replace security guards
- Can the Steelers win Super Bowl LII?
- How do molars and premolars differ?
- What is the history of Egypt
- How do players make money on Tumblr
- What happened to HP
- The Minecraft graphic style is copyrighted
- I love you because _______
- Have you ever avoided your crush entirely
- Is the taurine in energy drinks vegan
- Where can I buy laboratory-grown meat
- When is 24 hours not enough
- What are Hillary Clinton's most famous quotes
- Hillary Clinton is a racist at heart
- Can offend a blocked punt
- What is DHCP Snooping
- What was Charlie Chaplin's fortune
- What are some interesting RTI facts
- Who is your favorite rock star
- Why is Apple lagging behind in AI
- Don't think that celebrities are overrated
- How did the Polynesians discover new land
- Can you solve this particular problem of relativity