The Java object initialization and code block initialization sequence

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
class A
{
    public A(){
        System.out.println("Test!!!!!!!!!!!");
    }
}

class Demo19  extends A
{

    {
        x=7;//A block of code to initialize
        System.out.println("hello world!Test execution order code block!AAAAAAAAAAA");
    }
    public int x=1;//Here only declare the variable value does not initialize variables
    public int y=2;//Here only declare the variable value does not initialize variables
    
    public Demo19(){
    //super();
    //Equivalent to x=7
    //Then x=1;
    //y=2;
    //y=13;
    }
    public Demo19(int d_x,int d_y)
    {    //super();    
        //x=7;
        //x=1;
        //y=2;
        //y=13;
        x=d_x;
        y=d_y;
        //super();//If the display of the call might not be here initialization
    }
    //A block of code execution in accordance with the order of execution of a code block sequence
    //Execution time of a code block: executed when the object initialization!!!!!!!!!!!!!
    {
        y=13;//A block of code to initialize
        System.out.println("hello world!The test block of code execution order!BBBBBBBBBBBB");
    }
    
    //A block of code constructor
    
    public static void main(String[] args)
    {    
        //Here the initialization time just like the above initialization almost
        Demo19 demo=new Demo19();
        System.out.println("Test code block: x="+demo.x+"  ,y="+demo.y);
        
        //The initialization is equivalent to the operation of the above
        Demo19 demoOne=new Demo19(12,45);
        System.out.println("Test code block: x="+demoOne.x+"  ,y="+demoOne.y);
        
        System.out.println("The other is the order of the same! This is the more important");
        
        System.out.println("The use of a code block!");
        
    }
}

Abstract class initialization:

abstract class A
{
   String name;
   String id;
   
   public abstract void test();
}

abstract class B extends A
{
    //Sub classes can also be abstract classes, not realizing method of the parent class
}

class C extends A
{
    public  void test(){
        System.out.println("If it is not an abstract class that subclasses must implement the parent class method");
    }
}
abstract class D1
{
    /* Abstract class is the constructor, but this only sub class initialization */
    
    D1(){
        System.out.println("Test abstract class constructor!");
    }    
    static String name="What is your";
}
class D extends D1
{
    D(){
        System.out.println("Subclass superclass constructor call");
    }
    public void test(){
        System.out.println(this.name);
        
        System.out.println("If there is no abstract methods abstract class inheritance, then there is no problem");
    }
}
class Demo5
{
    public static void main(String[] args)
    {
        D d =new D();
        d.test();
        System.out.println("Abstract class!!!!!");
        System.out.println("Class does not support multiple inheritance!!!!!!!!!!!!!!!!");
    }
    
}



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Roger at November 16, 2013 - 11:57 AM