Static control flow in Parent to child Relationship - rahul00773/JavaConcepts GitHub Wiki
Whenever we are executing child class the following sequence of events will be executed automatically as part of static control flow.
- Identification of static members from parent to child. [1-11]
- Execution of static variable assignment and static blocks from Parent to child.[12-22]
- Execution of only child class main method. [23-25]
class Base{
static int x=10; //1 int x=0 //7 //12
static{ //2
m1(); // 13
System.out.println(“First Static Block”); ///15
}
public static void main(String[] args){ //3 m1(); // 13
System.out.println(“Main Method”);
}
Public static void m1(){ ////4 //14
System.out.println(j);// / j=0 //9 }
static int j =20; //5 // 12 j =20 // 16 }
class Deriver extends Base{ static int I=100; // 6. ///17
static{ //7 m2(); // 18 System.out.println(“Derived first Static Block”); ////20
}
public static void main(String[] args){ //8 m2(); // 13 ////23
System.out.println(“Derived Main Method”); ///25
}
Public static void m2(){ ///9 //14
System.out.println(y);// / j=0 //9 ///19///24 }
static{ //10 System.out.println(“Derived second Static Block”); ///21 }
static int y =200; //11 //22
}
// Output:
0 First Static Block 0 Derived first Static Block Derived second Static Block 200 Derived Main Method
##Note:
Whenever we are loading child class automatically parent class will be loaded. But whenever we are loading parent class child class won’t be loaded.[Because parent class members by default available to the child class whereas child class members by default won’t available to the parent]