This article analyzes the difference between member variables and local variables in Java with examples. Share it with everyone for your reference. The specific analysis is as follows:
Member variables: Private variables defined in this class belong to this class.
Create and use member variables to copy the code. The code is as follows: 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+",Gender"+p.Sex+",Age:"+p.age+",Age:"+p.Height);
}
}
Member variable initialization process
1. Initialization of class
For class initialization: Class initialization is generally only initialized once, and class initialization mainly initializes static member variables.
The compilation of a class determines the initialization process of the class.
The class file generated by the compiler mainly makes the following changes to the classes defined in the source file:
1) First declare member variables inside the class in the order in which static member variables are defined.
2) Initialize according to the initialization sequence of member variables in the original java class.
The corresponding conversion between a java class and the compiled class is as follows:
Source file:
Copy the code as follows: public class Person{
public static String name="Zhang San";
public static int age;
static{
age=20;
System.out.println("Initialization 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);
}
}
When the java source code is converted into a class file, it is converted into code similar to the following:
Copy the code as follows: public class Person{
public static String name;
public static int age;
public static String address;
static{
name="Zhang San";
age=20;
System.out.println("Initialization 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 sequentially according to the initialization sequence of the corresponding class member variables after conversion, so all static member variables are declared first and then assigned, and the order of assignment is also based on the order in which the static member variables are initialized in the source code. , Note: Defining a member variable and initializing it directly is equivalent to initializing it in a static code block, both in the order in which they are defined in the source code.
local variables
Local variable: Created in the method body, this variable cannot be accessed outside the method body.
Creation and use of local variables (local variables must be assigned a value, member variables may not be assigned a value)
Copy the code as follows: 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);
}
}
See the example and copy the code. The code is as follows: public class PassTest {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("a");
StringBuffer b = new StringBuffer("b");
a(a, b);
System.out.println(a);
System.out.println(b);
PassTest p = new PassTest();
pc();
}
static void a(StringBuffer a, StringBuffer b) {
a = a.append(b);
b = a;
}
}
According to the scope of use of local variables, the result should be ab, but in fact the output is indeed ab b. Why?
It's a question of passing parameter references. If it is a reference, what is passed should be a copy of the same reference.
b=a in method a changes the copy b reference to =a, but has no effect on b in main.
a = a.append(b); Mainly a.append(b); This sentence changes the value pointed by a reference, because a in main also points to the same object, so the output is ab b
If a = a.append(b); is changed to a = new StringBuffer("ab"); ab will be output
Look at the following two programs:
Procedure one:
Copy the code as follows: 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 two:
Copy the code as follows: 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 compiles without errors. When the second program is compiled, the following error will be prompted:
D:Programjavatest>javac Variable.java
Variable.java:9: Variable i may not have been initialized yet
if(j==i)
^
mistake
The reason why such an error occurs is because: member variables have default values (those modified by final and not static must be assigned explicitly), and local variables will not be assigned automatically.
The class body is divided into two parts. The variables defined in the variable definition part are called member variables of the class, and the variables defined in the method body and the parameters of the method are called local variables.
The difference between local variables and member variables
Local variables describe the attributes in the method body, while member variables describe the attributes in the object.
Member variables can be modified by public, protected, default, private, static, and final modifiers, while local variables can only be modified by the final modifier.
Member variables are created on the heap, and local variables are created on the stack.
Local variables have system default values. Local variables do not have system default values and must be assigned manually.
I hope this article will be helpful to everyone’s Java programming.