The Java program is run by the Java Virtual machine. Class from loading to virtual machine memory to unloading out memory, including
Load-----Link-----Initialize-----use------Uninstall
Links specifically include: validation-----Preparation-----Resolution
Load: Performed by the ClassLoader, looking up bytecode and creating a class object from these bytecode.
Link: Validates the bytecode in the class, allocates storage memory for the static domain and assigns default values, and resolves all references to other classes created by this class.
Initialize: The class has a base class, then initializes it, performing static initialization and static initialization blocks.
Class initialization time: The first use in the program is initialized.
First-time active use:
1. Creating an instance of a class
2. Accessing static variables of a class
3. Invoking a static method of a class
4. Reflection Invocation
5. Initializes the subclass of the class if the parent class is initialized to initialize the parent class first
6. The Startup class (which contains the main method) that is specified when the virtual machine starts initializes the class first
Initialization steps for the class:
1, the load connection is not loaded or linked.
2, there is a direct parent class and is not initialized then initialize the direct parent class first.
3, there is an initialization statement, which executes the initialization statement sequentially. (initialization statements include static statements and static blocks of code)
Note:
1. If it is a staticfinal compiler constant, it is not necessary to initialize the class to be read, but is not a compiler constant access to it will force the initialization of the class
Class A{public static final int b = 10;public static final int c = new Random (). Nextint (), Static {System.out.println ("class A Initialized ");}} public class Test{public static void Main (string[] args) {int i = A.B; does not cause initialization because B is a compile-time-constant int II = A.C; Back causes initialization, C cannot be determined at compile time}}
2. The static domain is not final, so access to it must be linked and initialized.
3. Active use of a class or interface is considered only when the static variable or method accessed is defined in the current class or interface.
Interface A{int A = 10;} Class AImp implements A{static {System.out.println ("class A is initialized");}} public class Test{public static void Main (string[] args) {int i = A.A; Does not cause initialization, the variable accessed is not defined in the current class}}
4. When you use. class to create a reference to a class, it is not automatically initialized. Initialization is deferred until the first call to a static method or a static domain is performed.
class A{public static int b = 10;static {System.out.println ("class A is initialized");}} public class Test{public static void Main (string[] args) {class Class1 = A.class; No output int aa = A.B; This sentence causes the output Class A to be initialized with the try {class Class2 = Class.forName ("a"); Comment out the above two sentences Output:a class is initialized} catch (ClassNotFoundException e) {e.printstacktrace ();}}}
Initialization of members in a class
1. Assigning an initial value to a variable in a class in Java you can assign values to a class member where it is defined (this is not possible in C + +), or it can be initialized by a constructor, and the constructor cannot prevent automatic initialization.
Class Test{int A; Test () {a = 10;} <span style= "font-family:arial, Helvetica, Sans-serif;" >a is first set to 0 and then to 7.</span>}
2. The order in which variables are initialized depends on the order in which the variables are defined. They are initialized before any methods (including constructors) are called.
class A{public int i = 10;public A () {System.out.println ("constructor" + i); i = 20; System.out.println ("constructor," + i); static {System.out.println ("Class A is initialized");}} public class Test{public static void Main (string[] args) {A A = new A (); Before executing the constructor, the hunger ratio is assigned to 10, and the constructor is then set to 20//output://a class is initialized//constructor 10//constructor 20}}
3. Initialization of static data, occupying only one part of the memory area. Static initialization occurs only at the necessary moment. The necessary moment refers to when the class is actively used.
4. The initialization order is first static and then non-static. The previous discussion of the initialization process only said static statements and static code blocks, in fact, there will be non-static member properties of the initialization, and is first initialized static reinitialization non-static. It is not possible to have a non-static member initialized while a static member has not been initialized.
a strange question.
Class singleton{//private static Singleton Singleton = new Singleton (); 1public static int counter1;public static int counter2 = 0;private Singleton () {counter1++;counter2++;} private static Singleton Singleton = new Singleton (); 2public static Singleton getinstance () {return Singleton;}} public class Test{public static void Main (string[] args) {Singleton Singleton = singleton.getinstance (); System.out.println ("counter1 =" + Singleton.counter1); System.out.println ("Counter2 =" + Singleton.counter2);}}
Output:
Put in position 1
1 Singleton Singleton =singleton.getinstance (); because it is a static method that calls the class, because the check is not yet loaded, it is then loaded, linked, and initialized sequentially.
2 singleton null Counter1 to 0 counter2 for 0 during the preparation of the link
3 initialization process, in sequence
3.1 Initialize private Staticsingleton singleton = new Singleton (), execute constructor, Counter1 1,counter2 to 1
3.2 Initialize public static intcounter1; There is actually no initialized value, so the previous value is 1
3.3 Initialize public static intcounter2 = 0; Assign 0 to Counter2 to 0
Outputs are 1 and 0
Counter1 = 1
Counter2 = 0
It's easy to understand when you put it in position 2.
Counter1 = 1
Counter2 = 1
Java class loading and initialization