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.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Chaitanya
  • 105
  • 9
  • Static blocks are executed at class loading time similar question [enter link description here][1] [1]: http://stackoverflow.com/questions/9130461/when-is-the-static-block-of-a-class-executed – Waqas Ikram Jul 15 '15 at 13:51

6 Answers6

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 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 Jul 15 '15 at 13:55
  • @Chaitanya - That depends on the reference being used. *static methods* cannot be overriden – TheLostMind Jul 15 '15 at 13:56
  • And what if I wanted to call static method of derived class first.? Maybe its illogical question but is it possible. – Chaitanya Jul 15 '15 at 14:18
  • @Chaitanya - First and second doesn't matter here. If you have a reference of derived class, you can call it at any time – TheLostMind Jul 15 '15 at 15:29
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.

Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35
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".

Tom Hughes
  • 201
  • 1
  • 3
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

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
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/]
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

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

Community
  • 1
  • 1
Waqas Ikram
  • 71
  • 2
  • 6