Member variables:The private variables defined in this class belong to this class.
Create and use member variables
| The code is as follows: |
Copy code |
|
Public class Person {
String name;
String Sex;
Int age;
Double Height;
Public static void main (String arges [])
{
Person p = new Person ();
P. name = "xiao huang ";
P. Sex = "male ";
P. age = 20;
P. Height = 1.7;
System. out. println ("name:" + p. name + ", sex www.111cn.net" + p. sex + ", age:" + p. age + ", age:" + p. height );
}
} |
Member variable initialization process
I. Class initialization
Class initialization: class initialization is generally only performed once, and class initialization is mainly used to initialize static member variables.
Class compilation determines the class initialization process.
The class file generated by the compiler mainly changes the class defined in the source file as follows:
1) declare the member variables in the class according to the definition sequence of static member variables.
2) initialize the member variables in the original java class.
The conversion between a java class and the compiled class is as follows:
Source File:
| The code is as follows: |
Copy code |
|
Public class Person {
Public static String name = "Zhangsan ";
Public static int age;
Static {
Age = 20;
System. out. println ("initialize age ");
}
Public static String address;
Static {
Address = "Beijing ";
Age = 34;
}
Public static void main (String [] args ){
System. out. println (name );
System. out. println (age );
System. out. println (address );
}
}
|
After the java source code is converted into a class file, it is converted into code similar to the following:
| The code is as follows: |
Copy code |
|
Public class Person {
Public static String name;
Public static int age;
Public static String address;
Static {
Name = "James ";
Age = 20;
System. out. println ("initialize age ");
Address = "Beijing ";
Age = 34;
}
Public static void main (String [] args ){
System. out. println (name );
System. out. println (age );
System. out. println (address );
}
}
|
The initialization sequence is executed in sequence based on the initialization sequence of the corresponding class member variables after the conversion. Therefore, all static member variables are declared first and then assigned values, in addition, the assignment order is also carried out according to the source code initialization sequence of static member variables. Note: Defining a member variable and directly initializing is equivalent to initializing in a static code block, they are all based on the order they are defined in the source code.
Local variable
Local variable: This variable is created in the method body and cannot be accessed in the method body.
Create and use local variables (local variables must be assigned a value, and member variables can be assigned no value)
| The code is as follows: |
Copy code |
Public class Person {
Public static void main (String arges [])
{
String name = "xiao huang ";
String Sex = "male ";
Int age = 20;
Double Height = 1.70;
System. out. println ("name:" + name + ", Gender" + Sex + ", age:" + age + ", age:" + Height );
}
} |
Look at the example
| The code is as follows: |
Copy code |
|
Public class PassTest {
Public static void main (String args []) {
StringBuffer a = new StringBuffer ("");
StringBuffer B = new StringBuffer ("B ");
A (a, B );
System. out. println ();
System. out. println (B );
PassTest p = new PassTest ();
P. c ();
}
Static void a (StringBuffer a, StringBuffer B ){
A = a. append (B );
B =;
}
} |
According to the scope of use of local variables, the result should be a B, but the actual output is actually AB B. Why?
The parameter reference should be passed. If the reference is passed, it should be a copy of the same reference.
In method a, B = a Changes the Copy B reference = a, but it does not affect B in main.
A =. append (B); mainly. append (B); this statement changes the value pointed to by a reference. Because a in main also points to the same object, the output is AB B.
If a = a. append (B); changes to a = new StringBuffer ("AB");
Let's look at the following two procedures:
Procedure 1:
| The code is as follows: |
Copy code |
|
Public class Variable
{
Int I;
Void test ()
{
Int j = 8;
If (j = I)
System. out. println ("equal ");
Else
System. out. println ("not equal ");
}
Public static void main (String [] args)
{
Variable v = new Variable ();
V. test ();
}
} |
Procedure 2:
| The code is as follows: |
Copy code |
|
Public class Variable
{
Void test ()
{
Int I;
Int j = 8;
If (j = I)
System. out. println ("equal ");
Else
System. out. println ("not equal ");
}
Public static void main (String [] args)
{
Variable v = new Variable ();
V. test ();
}
} |
The first program is normal and there will be no errors during compilation. The second program will prompt the following error during compilation:
D: Programjavatest> javac Variable. java
Variable. java: 9: Variable I may not be initialized
If (j = I)
^
1 Error
This error occurs because the member variable has a default value. (if it is modified by final and has no static value, it must be explicitly assigned.)
The class body is divided into two parts. The variables defined in the variable definition section are called class member variables. The variables and method parameters defined in the method body are called local variables.
Differences between local variables and member variables
Local variables describe the attributes in this method, and member variables describe the attributes in this object.
Member variables can be modified by public, protected, default, private, static, and final modifiers. Local variables can only be modified by final modifiers.
Member variables are created in the heap, and local variables are created in the stack.
The local variable is the default value of the system. If the local variable does not have the default value of the system, you must assign a value manually.