Tuesday, 1 May 2018




Static control flow parent to child relationship---------------.

Whenever we are executing java class the following sequence of Steps will be executed as the part of static control flow

1. Identification of static members from parent  to child.
2. Execution of static  variable assignments and static blocks from parent to child.
3. Execution of only child class main method.





package staticcontrolflowpc;

public class Base {
           
            static int i=10;
            static
            {
                        m1();
                        System.out.println("Base Static Block");
            }
           
            public static void main(String[] args) {
                        m1();
                        System.out.println("Base Main");
            }
           
            public static void m1()
            {
                        System.out.println(j);
            }
           
            static int j=20;

}


package staticcontrolflowpc;

public class Derived  extends Base
{
           
            static int x=100;
            static
            {
                        m2();
                        System.out.println("Derived First Static Block");
            }
           
            public static void main(String[] args) {
                        m2();
                        System.out.println("Derived Main");
            }
           
            public static void m2()
            {
                        System.out.println(y);
            }
           
            static
            {
                        System.out.println("Derived Second Static Block");
            }
           
            static int y=200;

           
}



o/p:----------------------------------


0
Base Static Block
0
Derived First Static Block
Derived Second Static Block
200
Derived Main

No comments:

Post a Comment