Java initialization

Sort out the basic things, there may be mistakes, please advise

Before talking about instantiation and initialization, let's first understand the mechanism of JVM loading classes.

The class loading process, in layman's terms, is that after the code implemented by the developer is compiled, a *.class file is generated.

The process of loading it into the memory through the virtual machine (JVM) is the class loading mechanism.

The JVM loads classes in three parts:

1. Load

Loading is to load the class into the memory, that is, the method area. This process will not execute any statement. But in this process, non-static variables will not be loaded.

2. Links

This process is divided into three parts:

1) Verification: that is, to verify whether the loaded class meets the requirements of the JVM

2) Preparation: Allocate space for static variables and set initial values. The initial value mentioned here is the initial value of the value type, not the value we assign. E.g:

static int i=1; in this process, only memory is allocated to i, and then i is assigned the initial value of the type, that is, i=0 now;

3) Parsing: Convert character calls to direct calls.

After the above steps, we can proceed with initialization.

3. Initialization

In this step, the real Java code is executed.

Assign a value to a static variable, i.e. 1 above, and assign it to i here.

So now the question comes, why do you say that static variables will be assigned values ​​here? So what about non-static variables and where does it go?

Personally understand here that the process of initialization is accompanied by the instantiation of the class. But static variables do not require instantiation of the class. That is to say, if we want to initialize a member variable, then we should first instantiate the class where the variable is located, that is, a new object, allocate a memory space to the class, and allocate the variable to the memory. Then assign it. This is the specification of the Java Virtual Machine.

<pre name="code" class="java">package controller;

public class Base {
	 	int a;  
	    static int b=3;  
	    static int c = 1;  
}

 
 
package controller;

public class Test {
	public static void main(String[] args) {
		System.out.println(Base.b);
		Base base=new Base();
		System.out.println(base.a);
	}
}

In the above code, we can see that for member variables, we must new an object, that is, allocate space. Then implement initialization.

Then we have another question?

What about initialization between member variables and local variables?

package controller;

public class Test {
	
	int a;
	
	public static void main(String[] args) {
		int b;
		Test test=new Test();
		System.out.println(test.a);
		System.out.println(b);
	}
}
At this time, b cannot be initialized, and the program reports an error.

This is because when we new, the constructor will be called, and then the constructor will assign member variables, but for local variables, the constructor cannot be called, because the constructor itself is also a local method of the class. So when we directly define local variables without assigning them, the virtual machine cannot initialize them. So we have to manually assign values ​​to local variables.

Suddenly I felt that there was still a lot I didn't understand. . . . . . .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325592367&siteId=291194637