Member variables and local variables of Java variables and their operation mechanism

Java language divides variables into member variables and local variables according to the different positions of variable definitions. Member variables refer to the variables defined in the class (outside the method), and local variables refer to the variables defined in the method. At the same time, member variables are divided into class variables and instance variables according to whether they are modified with static. Local variables are divided into formal parameters, method local variables and code block local variables according to different definition positions, as shown in Figure 1.

Figure 1

In addition to following the naming standard, the naming of variables should also see the text and meaning, and the purpose of the variable can be understood through the name, which is conducive to subsequent code modification and improve efficiency.

Member variables: member variables are divided into class variables and instance variables. Class variables exist from the preparation stage of the class until the class is destroyed by the system. Class variables are only owned by the class, and member variables do not own class variables; Instance variables exist from the time the instance of this class is created until the instance is destroyed. There are two ways to access class variables: class Class variables and instances Class variables, but because the instance does not have class variables, it actually accesses class variables. Therefore, when an instance is used to modify a class variable, the class variable will be affected, and other objects will see changes when accessing this variable. There is only one way to access instance variables, instance Instance variable.

Local variables: local variables are divided into formal parameters (valid in the whole method), method local variables (valid from the beginning of definition to the end of method), code block local variables (valid from the beginning of definition to the end of code block). Local variables exist from initialization to the end of method.

The initialization and operation mechanism of member variables and temporary variables are described as examples below:

Member variables:

I wrote a dog class that contains two member variables, the class variable nose and the instance variable name. As shown in Figure 2.

Figure 2

In the main method of the test class, I started a series of operations on the dog class. The code is shown in Figure 3.

Figure 3

Dog dog = new dog(), if the system uses the dog class for the first time, the system needs to load this class and initialize it. In the preparation phase of the class, the system allocates space for the class and initializes the default value of the class variable. As shown in Figure 4.

Figure 4

After the class is loaded, the system will create a dog variable, allocate memory space and initialize the instance variable. At this time, the stack memory is shown in Figure 5.

Figure 5

dog. Name = "little black dog". After executing this code, I assign a little black dog to the name variable of the dog object. At this time, the memory situation is shown in Figure 6.

Figure 6

Execute dog dog2 = new dog(); At this time, the system already has a dog class. There is no need to load the dog class. At this time, the dog object dog2 is created, the system allocates memory for the dog2 object, and the name variable of the initialization dog2 is null. The memory condition is shown in Figure 7.

Figure 7

dog2. Name = "little white dog"; Execute this code to assign a value to the dog2 object name, which is consistent with dog1.

dog2. Nose = 1. At this time, although the value of nose is changed through the dog2 object, because nose belongs to the class itself, the value of class variable nose is changed. At this time, accessing nose with dog1 will get the value of 1 As shown in Figure 8.

Figure 8

To sum up: Java member variables are divided into class variables and instance variables. Class variables are initialized when the class is loaded, and instance variables are initialized when the object is instantiated. Class variables belong to the class itself and are shared by all instances, and instance variables belong to instance private.

Local variable: after the local variable is defined, it can only be used after display initialization. The system will not initialize the local variable. This means that after defining a local variable, the system does not allocate memory space for the variable. The system will not allocate memory for the local variable until the program assigns an initial value to the variable. The local variable does not belong to any class or instance. It is stored in the memory stack of the method. If the local variable is a basic type, the corresponding memory directly saves the value. If it is a reference type, it saves the address and references the actual object or array of the variable through the address.

Because local variables exist in the method stack and the method is destroyed at the end of the method, there is no need for garbage collection mechanism. Its life cycle starts at initialization (not at definition) and is destroyed at the end of the method.

Summary:

Member variables do not need display initialization. The system performs default initialization in the preparation phase of a class (initializing class variables) or when creating an instance of the class (initializing instance variables). Local variables must be display initialized, otherwise they cannot be accessed.

The member variable and local variable can have the same name. If they have the same name, the member variable needs to be accessed in the method. You can use this Variable name. (Note: This represents the object currently being created in the construction method and the object calling the method in the normal method).

Depending on the scope of different variable types, you should reasonably choose which variables to use. For example, member variables that can use local variables are stored in the heap, which expands the lifetime and scope of variables. Expanding the lifetime leads to greater memory overhead, and expanding the scope is not conducive to improving the cohesion of the program.

Reference: Java crazy handout

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>