Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is sub class' static code getting executed?

Tags:

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?

like image 596
AV94 Avatar asked Feb 02 '16 10:02

AV94


People also ask

When the static block will get loaded and executed?

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.

Do you know static block in Java when is it getting executed?

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.

Do static blocks get inherited?

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.

How many times static block is executed in Java?

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.


2 Answers

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:

  1. java triggers load of Sub

  2. JVM has to load SuperClass in order to load Sub

  3. So we see their static initializers run, in order (SuperClass, then Sub):

    super sub 
  4. java tool calls main

  5. Code in main calls new SuperClass:

    Super 
  6. Code in main calls f.c()

    2 
  7. 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.

like image 194
T.J. Crowder Avatar answered Oct 28 '22 08:10

T.J. Crowder


Because your main() method is a member of Sub, that class needs to be loaded for your program to run.

like image 30
Ted Hopp Avatar answered Oct 28 '22 10:10

Ted Hopp