JAVA member variables and local variables (detailed explanation)



JAVA variables can be divided into two categories: Member variables and Local variables.

Classification of member variables and local variables

Specific breakdown, member variables can be divided into two categories:Instance variables and Class variables< /span> As shown below:. code block local variables and Method local variables , Formal parameters
Local variables can be divided into three categories: .

Insert image description here



The difference between instance variables and class variables

The two types of variables are very different.The member variables related to a certain instance (object) are instance variables, The member variables related to the entire class are class variables.
Class variables and instance variables have the following differences:
1. Different life cycles :
Class variables are created during the preparation stage of the class. At this time, the system has not created the corresponding object. Therefore, class variables are created earlier than instance variables. Class variables will be destroyed together when the class is destroyed, and Instance variables will be destroyed together when the corresponding object is destroyed Destroy. Because the class must be destroyed when all objects are destroyed, the death time of class variables is also later than that of instance variables. It can be summarized as: The life cycle of a class variable is basically the same as the life cycle of the class, and the life cycle of the instance variable is basically the same as the life cycle of the corresponding object a>.

2. Different access methods:
The syntax for accessing class variables is: class.class variable name .
The syntax for accessing instance variables is: Reference variable name.Instance variable name.
Although class variables can also be accessed using reference variable name. Class variable name , but the accessed class variable It is not related to the object itself. At the bottom of Java, this method will also be replaced by: Class.Class variable name access method. So essentiallythis access method is a bad design of Java and is not recommended.

3. Different scopes:
Class variables are common to all objects, All objects access the class Variables all access the same memory area. Therefore, the scope of a class variable is the entire class. And instance variables are unique to a certain object. They are state attributes of a certain object. Changing the instance variables of an object will not affect other objects.

4. The storage locations in memory are different:
Class variables are stored in a separate area related to the class, when all objects access class variables, they access this area. And instance variables are stored in the area where the own object is located. Each object needs its own area to store its own instance variables.



The difference between three types of local variables

Local variables can be divided into three categories according to their definition positions:
1.Formal parameters : Variables defined when defining the method signature.
2.Method local variables: Variables defined in the method body.
3.Code block local variables: Local variables defined in the code block.

The following will explain their differences:1. Whether explicit initialization is required :Only the formal parameters do not need to be explicitly initialized, because the initialization of the formal parameters is the parameters passed by the caller when the corresponding method is called. To be done. Both method local variables and code block local variables need to be explicitly initialized, only define these two kinds of local variables, but Without initialization, the system will not generate the corresponding space in the memory. Explicit initialization is required before these two local variables will have specific space to store values ​​in the memory.

2. The scope and lifetime are different:
Formal parameters: Valid within the entire method.
Method local variable: It is valid from the time the variable is initialized to the end of the method.
Code block local variables: Valid from initialization in the code block to the end of the code block.



The difference between member variables and local variables

has the following differences:
1. Whether the system will initialize it by default:
For member variables, the system will It is automatically initialized during the class generation or object creation phase.Even if the member variable is not explicitly assigned a value, the system will allocate memory space for it and attach a zero value. But local variables must be explicitly initialized except for formal parameters (the initialization of formal parameters is completed by the caller through the passing of actual parameter values), otherwise the system will not Allocate memory space for it.

2. Different locations in memory:
Member variables are stored in heap memory, and Corresponding class or object binding. Local variables are bound to methods and stored together in stack memory. When the method execution ends, all local variables will be destroyed (code block local variables will be When the code block ends, it will be popped out of the stack and destroyed).



Problem with variables with the same name

It is best to avoid variables with the same name, but sometimes variables with the same name may occur.
The following are the rules for variables with the same name:
1. Member variables and local variables have the same name :
At this time, the member variables will be overwritten by local variables, that is, although the member variables exist, they cannot work within the method. Ifyou want to use an overridden member variable within this method, you must use this or the class name as the caller to limit access. With the addition of the main tone, the program can tell that the variable is a member variable.

2. Member variables with the same name are not allowed:
Even if one is a class variable and the other is an instance variable, the same name is not allowed. , otherwise program ambiguity will occur.

3. Method local variables cannot have the same name, nor can formal parameters have the same name, nor can method local variables have the same name as formal parameters:
In fact, it is easy to understand, because their life cycles overlap. If they have the same name, the system does not know which memory area you want to access.

4. Code block local variables in different code blocks can have the same name:
This is because when the code block ends, the code block local variables die. , so their life cycles do not overlap and there will be no ambiguity.

5. If you define code block local variables first and then define method local variables, it is allowed:
This is also caused by the mismatch of life cycles. The local variables of the code block die first, and the local variables of the subsequent methods with the same name will not diverge.



Variable usage rules

Although defining member variables directly can solve the problem, using member variables indiscriminately will make the program confusing.
Member variables have a larger scope and lifetime, which is not conducive to the cohesion of the program and brings much greater overhead.
The program hopes that the influence between each module will be as small as possible, so do not use member variables if you can use method local variables, and do not use method local variables if you can use code block local variables.

When to use member variables

You can consider using member variables if one of the following conditions is met:
1.This variable is used to describe the inherent information of a class or object , such as height, weight, etc.

2.This variable is used to save the real-time status of the class or instance when it is running., such as the backgammon board information, which will be updated as the chess is played. The process is constantly changing, and this information is naturally bound to classes or objects.

3.If the value of a variable will be shared between multiple methods, you should use member variables.

Guess you like

Origin blog.csdn.net/qq_983030560/article/details/130895152