Object instantiation

Create an instance of an object includes: class and instance initialization initialize 
1. To create an instance of a class required to load and initialize this class, main method where you need to be loaded and initialized
2. a subclass to initialize need to initialize the parent class
3 One class initialization is performed <clinit> () method
<clinit> method of displaying a static variable assignment code and static code blocks
class static variables and the static display codes assigned code blocks of code executed sequentially from top to bottom
<clinit> method performs only a
4. examples of the initialization is performed <init> method
<init> method of displaying the code assigned by the block non-static and non-static instance variables, a corresponding constructor code.
Non-static instance variables are displayed and non-static assignment of code block execution order from top to bottom, while the configuration corresponding to the last code executed
each time you create an object instance, call the corresponding constructor is the corresponding <init> method performed
<init> the method of the first line is super () or super (argument list), i.e., the corresponding <init> method of the parent class

Class load (class initialization): 
1, the class loader is the clinit () method (called class constructor) after execution of a Java program is compiled bytecode file, the clinit () method of static variables and code block composition.
2, subclass loaded first need to load the parent, if the parent class for the interface. Clinit will not call the parent's method. A class can not clinit method.
3, the order of execution of clinit method: the parent class static initialization, the parent class static block of code, static initialization subclass, the subclass static code block.
4, clinit () method is executed only once.

Process object instance (instance initialization):
1, the object is to perform the instantiation process init () method (called instance constructor) after the Java program is compiled bytecode file, init () method of a non-static variable , non-static configuration and a corresponding code block composed.
2, init () method can be overloaded plurality, there are several constructors have several init () method, each creates an instance, which calls a constructor will call the corresponding init () method.
3, the order init () method is: the parent class variable initialization, the parent class code block, the parent class constructor, variable initialization subclass, the subclass code block, the sub-class constructor.
the clinit () take precedence over the init () method is performed, so that the entire sequence is:
parent class static initialization, the parent class static block of code, sub-class static initialization, the subclass static block of code, the parent class of non-static initialization, the parent class non-static block of code, the parent class constructor, the non-static initialization sub-class, sub-class non-static code block, the sub-class constructor.

Static code block only loads and static class variables loaded. Only instance of an object creation: variables and initialization code blocks and constructors will be performed (create an object instance will first trigger class loading)

class loader: Execute <clinit>
Create an object instance: Perform << init>, but will first create a class instance of the class before loading and executing <clinit>.
Class initialization: performing <clinit> method, <clinit> method of displaying a static variable assignment code and static code blocks, <clinit> method is performed only once
instance initialization: performing <init> method, <init> method variables of a non-static instance show non-static assignment code and code block, a corresponding constructor code.

Trigger class loader:
1. Create a new instance of the object type (such as new new, reflection, serialization) to
be variable and this triggers initialization code blocks and performing a constructor, this instance is initialized, the other six are based initialization
2. when a static method call type (i.e., performed in the bytecode instructions invokestatic)
3. calling a static field or an interface type, or assignment is performed (i.e., in the bytecode, implementation of these static field getstatic, or putstatic instruction),
but with a modified final static field it has been assigned except the (String basic type, type of packaging is not included), which is initialized to a compile-time constant expressions
reflection 4. a method of call JavaAPI ( For example java.lang.Class method (the Class.forName) call, or other classes java.lang.reflect package method)
when 5. initialize a derived class (Java virtual machine specification explicitly require a class initialization, it must be done in advance superclass initialization operation, the interface exceptions)
6. the class contains the JVM startup start time of the main method.
7. For static fields, only the direct definition of the class of this field will be initialized, and therefore referred to by its subclasses static fields defined in the parent class, it will only trigger initialization of the parent class without triggering initialize the subclass.

Loading order: static variables and initialization block is initialized in accordance with their defined order in the class. Also, variables, and initialization block also follow this rule.
1. The parent - Static variable
2. parent class - static initialization block
3. subclass - Static variable
4. subclass - static initialization block
5. The parent - Variable
6. parent class - initialization block
7 The parent class - constructor
8. subclass - variable
9. subclass - initialization block
10. subclass - constructor

Polymorphic 
method overrides @Override
which methods can not be rewritten
1, Final method
2, a static method
3, private and other subclasses invisible method
polymorphism object
1, Subclasses override the parent's method by calling subclass object must be a subclass rewritten code
2, the default non-static method call object is this
3, this object is an object in the constructor or <init> method is being created, so by the child non-static method of the parent class is triggered when the class is instantiated call load,
call the object is a subclass of this object method call subclass is overridden.
When a subclass if the parent class is instantiated, it will trigger the parent class is loaded during the initialization process if the parent class method overridden by subclasses, and calls the subclass.
Type variable to point to a subclass of type object, when accessing the member variables is the parent class member variables accessed through the parent class reference.


Parent code:
/ ** 
 * initialize the parent class <the clinit> 
 *. 1, J = Method () 
 * 2, a static code block parent class 
 * 
 * instance of the parent class: 
 *. 1, Super () foremost 
 * 2, i = test ( ); 
 * 3, non-static block parent class 
 * 4, no reference structure of the parent class (last) 
 * 
 * 
 * in front of a non-static method is actually a default object the this 
 * the this constructor or <init>, which represents the object is created, since here the object is created son, so 
 * Test () is a subclass overrides execution code (object-oriented multi-state) 
 * where i = test () is executed in a subclass overrides test () method 
 * @author Yuan 
 * @date 2019/11/23 
 * / 
public  class father {
     / ** 
     * = I test () is executed to rewrite the subclass test, because the parent is triggered subclass instantiation class loader 
     * / 
    Private  int I = Test ();
     Private static int j = method();

    static {
        System.out.print("(1)");
    }

    public Father() {
        System.out.print("(2)");
    }

    {
        System.out.print("(3)");
    }

    public int test() {
        System.out.print("(4)");
        return 1;
    }

    public static int method(){
        System.out.print("(5)");
        return 1;
    }

    public static void main(String[] args) throws Exception {
        new Father().test();
    }
}

 

Subclass code:

/ ** 
 * initialize subclass <the clinit> 
 * 1, J = Method () 
 * 2, a static class parent blocks 
 * 
 * initialize Parent Class: (5), (1) 
 * initialize subclasses: (10) , (6) 
 * 
 * instance of a subclass of <the init>: 
 *. 1, Super () foremost, Super behalf of the parent class is initialized not create a parent class instance, is loaded parent 
 * 2, I = Test (); 
 *. 3 , non-static block subclass 
 * 4, subclass constructor with no arguments (last) 
 * 
 * son because two objects created, so example method <init> performed twice, but the <clinit> method only once 
 * 
 * @author Yuan 
 * @date 2019/11/23 
 * / 
public  class Son the extends Father {
     Private  int I = Test ();
     Private  static  int J = Method ();

    static { 
        of System.out.print ( "(. 6)" ); 
    } 

    public Son () {
         // Super (); write or not write all exist in the subclass constructor will call the parent class constructor 
        System. Out.print ( "(. 7)" ); 
    } 

    { 
        of System.out.print ( "(. 8)" ); 
    } 

    @Override 
    public  int Test () { 
        of System.out.print ( "(. 9)" );
         return . 1 ; 
    } 

    public  static  int Method () { 
        of System.out.print ( "(10)" );
         return . 1;
    }

    public static void main(String[] args) throws Exception {
        Son s = new Son();
        System.out.println();
        Son son = new Son();
    }

}

Output:

(5)(1)(10)(6)(9)(3)(2)(9)(8)(7)
(9)(3)(2)(9)(8)(7)

 

Guess you like

Origin www.cnblogs.com/yuancoco/p/11922489.html