I have written the following code and created object for the super class.
class SuperClass{ static int a=2; static int b(){ return 2; } int c(){ return 2; } SuperClass(){ System.out.println("Super"); } static { System.out.println("super"); } } public class Sub extends SuperClass{ Sub(){ System.out.println("Sub"); } static { System.out.println("sub"); } static int b(){ return 3; } int c(){ return 3; } public static void main(String ax[]){ SuperClass f =new SuperClass(); System.out.println(f.c()); System.out.print(SuperClass.b()); } }
When I checked the output, it is as follows:
super sub Super 2 2
I know that static block is executed only when object of the class is initialized or any static reference is made. But here, i did not make any of these to Sub class. then why do i see "sub" i.e. sub class' static block output?
Static blocks execute when the class is loaded into the memory whereas instance blocks execute only when instance of the class is created. 5. 'this' keyword cannot be used in the static block whereas this keyword can be used in the instance block.
A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.
Static initialzier blocks are not inherited . Static blocks are executed when class is loaded since your base class extends a super class , even the super class definition will be loaded by JVM when referring to your class. Lastly,the instance block can also never be inherited.
1 Answer. Show activity on this post. Code inside static block is executed when the class is loaded by JVM first time. If you load again then the static block will not execute.
I know that static block is executed only when object of the class is initialized or any static reference is made. But here, i did not make any of these to Sub class.
Your code doesn't, but in order for that main
to run, Sub
has to be loaded. So the static initializer for it is run.
E.g., I'm assuming you ran it like this:
java Sub
The java
tool has to load Sub
to call Sub.main
. That's the static reference (access, really) causing the static initializer to run. (If you ran it in an IDE, the IDE will do the java
tool part, but the result is the same.)
So here's what happened:
java
triggers load of Sub
JVM has to load SuperClass
in order to load Sub
So we see their static initializers run, in order (SuperClass
, then Sub
):
super sub
java
tool calls main
Code in main
calls new SuperClass
:
Super
Code in main
calls f.c()
2
Code in main
calls SuperClass.b
:
2
As Holger helpfully points out, this is covered by the JVM specification in §5.5 - Initialization and the related §5.2 - Java Virtual Machine Startup:
Initialization of a class or interface consists of executing its class or interface initialization method (§2.9).
A class or interface C may be initialized only as a result of:
...
If C is a class, the initialization of one of its subclasses.
If C is a class, its designation as the initial class at Java Virtual Machine startup (§5.2).
That second-to-last bullet point covers SuperClass
, and the last bullet point covers Sub
.
Because your main()
method is a member of Sub
, that class needs to be loaded for your program to run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With