Java variables can be divided into local variables and member variables. Local variables can be divided into three types: shape parameters, local variables in the method, and local variables in the code block.
Variables defined in the class are called member variables (fields). Non-static variables or instance variables are called without static modification. Static variables or class variables are called if static modification is used.
For instance variables, each time an instance is created for this class, a memory space needs to be allocated to the instance variables.
Instance variable initialization memory allocation:
Let's take a look at the following code:
Public class test {string name; int age; public test (string name, int age) {system. out. println ("constructor"); this. name = Name; // Weight = 2.5; this. age = age ;}{ system. out. println ("non-static constructor"); Weight = 2.0;} Public String tostring () {system. out. println (weight); Return "";} double weight = 2.3; public static void main (string [] ARGs) {test Ti = new test ("X", 4 ); ti. tostring ();}}
Q: What is the output here? What is weight?
A: The output result is:
"Non-static constructor"
"Constructor"
2.3
Is it strange for someone to Output 2.3?
The execution sequence of the Code is as follows:
1. Apply for weight space in stack memory. Note that the default value is 0.0.
2. Execute the non-static constructor. Note that only the allocated space will not be assigned a value, so this code does not make any sense.
3. Execute the constructor assignment, and assign values according to the specified initial value and non-static constructor order. That is to say, weight is assigned only when new test () is executed.
Grant 2.0 first, then 2.3, and then execute the remaining methods in the constructor. (The final value is 2.5 instead of 2.3)
Therefore:The initialization process of instance variables is to apply for space in the heap memory. Whenever a value is assigned, the value is assigned only when the constructor is called, values are assigned in the order of initialization values or non-static initialization blocks. Finally, values are assigned to constructors.
Class variable initialization memory allocation:
The initialization process of class variables is similar to that of strength variables, except that the three steps of applying for space-constructor-assignment are simplified to apply for space-assignment. Let's take a look at the following example:
Class Price{ final static Price INSTANCE = new Price(2.8); static double initPrice = 20; double currentPrice; public Price(double discount){ currentPrice = initPrice - discount; }}main(){ system.out.println(Price.INSTANCE.currentPrice);}
Q: What is the output here?
A: The output result is:
-2.8
Does someone think the output-2.8 is strange?
The execution sequence of the Code is as follows:
1. Apply for the space of instance and initprice in the stack memory;
2. assign values in sequence. Run new price (); first. The default value of initprice is 0. So currentprice =-2.8
3. The value of initprice is 20.