0
class A { static{ get(); } static void get(){ System.out.println("HELLO"); } } class B extends A { public static void main(String[] args) { } } 

Output:

HELLO

How is static method of class A getting called. I can understand static method of B getting called but not of A.

1

6 Answers 6

3

Because B extends from A, if the B class is loaded, then the A class has to get loaded, and if it is loaded, its static initializer block must be called.

Sign up to request clarification or add additional context in comments.

4 Comments

So if I write a static method in class B also then which one will be executed first ? A is instantiated first then B. ?
@Chaitanya - That depends on the reference being used. static methods cannot be overriden
And what if I wanted to call static method of derived class first.? Maybe its illogical question but is it possible.
@Chaitanya - First and second doesn't matter here. If you have a reference of derived class, you can call it at any time
1

Because, since B is inheriting A, B has to load A when main() executes. This activates the get() function since it is in the static block.

Comments

1

To execute the main method of class B, class B has to be loaded.

Class B extends class A, so to load B, class A must be loaded.

When class A is loaded, its static initializer is invoked, calling the get() method and printing "HELLO".

Comments

0

In inheritance class loading is happens from top to bottom i.e. Parent to child. It first loads the parent class in your case A and then loads the child class in your case B. And when class loads its static block get called first that why its printing HELLO

Comments

0

All Static blocks are executed at class loading time ... duplicate question When is the static block of a class executed?

Comments

0

During compilation the compiler knows that B is dependant on A. It passes the same data to the JVM. During loading phase of class B, the JVM reads metadata sent by the compiler and finds out that B is dependant on A and hence loads (and runs static initializers) of A then continues to load B and then initializes B.

If we had 2 classes Sample and Test extends Sample the Class Constant Pool (part of byte code ) for Test would have :

Constant pool: #1 = Class #2 // Test #2 = Utf8 Test #3 = Class #4 // Sample <---- reference #4 = Utf8 Sample #5 = Utf8 <init> #6 = Utf8 ()V #7 = Utf8 Code #8 = Methodref #3.#9 // Sample."<init>":()V <-- init Sample #9 = NameAndType #5:#6 // "<init>":()V #10 = Utf8 LineNumberTable #11 = Utf8 LocalVariableTable #12 = Utf8 this #13 = Utf8 LTest; #14 = Utf8 main #15 = Utf8 ([Ljava/lang/String;)V #16 = Utf8 args #17 = Utf8 [Ljava/lang/String; #18 = Utf8 SourceFile #19 = Utf8 Sample.java 

If you run java with verbose:class option then grep it, you will be able to see that the dependant class is being loaded.

java -verbose:class Test | grep 'Sample' [Loaded Sample from file:/Users/XXXX/Workspaces/SampleTest/Sample/bin/] <== Sample loaded first because test depends on Sample. [Loaded Test from file:/Users/XXXX/Workspaces/SampleTest/Sample/bin/] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.