The function of parsing Java static keyword from memory address

Differences between static member variables and non static member variables

Take the following example as an example

Understand the execution process of the whole program by drawing the memory analysis diagram

Perform the first sentence of the procedure: cat sid = 100; The SID here is a static member variable. The static variable is stored in the data Seg, so first allocate a small space Sid in the data area. After the first sentence is executed, the SID contains a value of 100.

The memory layout diagram is shown below

Next, the program executes to:

Here, call the constructor cat (string name) of cat class. The constructor is defined as follows:

When calling, first allocate a small memory mm in the stack memory, which contains the address of the instance object of cat class that can be found in the heap memory. Mm is the reference object of cat class object in the heap memory. This constructor declares a formal parameter variable of string type, so "Mimi" is passed to the constructor as an argument. Since the string constant is allocated in the data area, there is a small piece of memory in the data area to store the string "Mimi". The memory distribution at this time is shown in the following figure:

When calling the construction method, first allocate a small space to the formal parameter name in the stack memory, named name, and then pass the string "Mimi" as an argument to name. The string is also a reference type. Except for the four types and eight basic data types, all others are reference types, so it can be considered that the string is also an object. So this is equivalent to passing the reference of the object "Mimi" to name, so now name points to "Mimi". Therefore, the memory layout is shown in the following figure:

Next, execute the code in the constructor body:

This here refers to the current object and the cat in the heap memory. Here, the value contained in the name in the stack is passed to the name attribute of the cat object in the heap memory. Therefore, the value contained in the name can also find the string object "Mimi" in the data area. At this time, the name is also a reference object of the string object "Mimi", The string object "Mimi" located in the data area can be found through its attribute value. The memory distribution at this time is shown in the following figure:

Next, execute another code in the method body:

Here, the SID value is passed to the ID, so the ID value is 100. After the sid is passed, add 1. At this time, the SID becomes 101. The memory layout is shown in the following figure.

At this point, after the constructor is called, all the memory space occupied by the local variables allocated to the constructor will disappear, so the memory of name in the stack space will disappear. The reference to the string object "Mimi" in the data area in the stack memory also disappears. At this time, only the reference to the string object "Mimi" in the heap memory does not disappear. The memory layout is shown in the following figure:

Next:

This is the second call to construct method cat (). The whole call process is the same as the first. After the call, the memory layout is shown in the following figure:

The last two sentences are printed by calling the info () method. The printing results are as follows:

Through this program, we can see the role of the static member variable SID, which can be counted. Whenever a cat new comes out, write it down. Let it add 1 to itself.

After the program is executed, the whole layout in memory is shown in the figure above. It continues until the moment before the main method call is completed.

Here, call the constructor cat (string name) to create two cats. First, allocate two small spaces Mimi and Pipi in the stack memory, which contain the addresses of the two cats. Mimi and Pipi correspond to the references of the two cats in the heap memory. The construction method here declares variables of string type. String constants are allocated in the data area, so the passed string Mimi and Pipi will be stored in the data area. Therefore, two small pieces of memory for storing strings Mimi and Pipi are allocated in the data area, which contain strings "Mimi" and "Pipi". Strings are also reference types. Except for the four types and eight basic data types, all other data types are reference types. Therefore, it can be considered that the string is also an object.

Here is new. The two cats have their own ID and name attributes, so the ID and name here are non static member variables, that is, there is no static modification. Therefore, each new cat has its own ID and name, that is, the non static member variables ID and name have a separate copy for each object. However, for static member variables, there is only one copy. No matter how many objects are new, even if there is no new object, one copy of static member variables will be retained in the data area. Like the SID here, the sid is stored in the data area. No matter how many cats come out of new in the heap memory, there is only one sid, and only one sid is reserved in the data area.

Static member variables belong to the whole class, and they do not belong to a specific object. So how do I access the value of this static member variable? First, any object can access the static value, and the same memory is accessed when accessing. Second, you can access the static value even if there is no object. You can access the static value through "class name. Static member variable name", so you can see a class name plus "." In addition, if there is something behind it, it must be static, such as "system Out ", here is through the class name (system class) plus". " To access the out, so the out must be static. If a class member is declared static, it can be accessed before any object of the class is created without referring to any object. The most common example of a static member is main (). Because main () must be called at the beginning of program execution, it is declared static. Variables declared as static are essentially global variables. When an object is declared, a copy of the static variable is not generated, but all instance variables of the class share the same static variable. For example, declare a static variable count as the count of a new class instance. Methods declared as static have the following restrictions: (1) they can only call other static methods. (2) they can only access static data. (3) , they cannot reference this or super in any way. If you need to initialize your static variable through calculation, you can declare a static block, which is executed only once when the class is loaded. The following example shows

Once the usestatic class is loaded, all static statements are run. First, a is set to 3, then the static block is executed (printing a message), and finally, B is initialized to a * 4 or 12. Then call main (), main () to call meth () and pass the value 42 to X. The three println () statements refer to two static variables A and B, and the local variable x. Note: it is illegal to reference any instance variable in a static method. The following is the output of the program:

Static methods and variables can be used independently of any object outside the class that defines them. For example, if you want to call a static method from outside the class, you can use the following general format: classname. Method(), where classname is the name of the class and the static method is defined in the class. As you can see, this format is similar to that of calling non static methods through object reference variables. A static variable can be accessed in the same format -- the class name plus the dot operator. This is how Java implements a controlled version of global functions and global variables. Summary: (1) static members cannot be accessed by the instance created by their class. (2) if members without static modification are object members, they belong to each object. (3) Java static: as a modifier, it can be used to modify variables, methods and code blocks (but it must not modify classes).

(1) Modification variable: an attribute shared by all objects of a class, also known as class variable. This is similar to global variables in C language. Class variables are initialized when the class is loaded, and are initialized only once. In the program, any object modifies a static variable, and other objects see the modified value. Therefore, class variables can be used as counters. In addition, J The AVA static variable can be accessed directly with the class name without requiring an object. (2) Modification method: a function shared by all objects of a class is called static method. Static methods can also be accessed directly by class name without requiring objects. Therefore, non static variables and non static methods cannot be accessed directly in static methods, and keywords such as this or super cannot appear in static methods. (3) Modify java code block: modify an independent code block in a class with static, which is called a static code block. The static code block is executed when the class is loaded for the first time, and only once. The static code block has no name, so it cannot be called explicitly, but only by the virtual machine when the class is loaded. It is mainly used to complete some initialization operations 。 (4) Talk about class loading: when the JVM uses a class for the first time, it will go to the path specified in the classpath to find the bytecode file corresponding to the class, read it into the JVM and save it. This process is called class loading. It can be seen that variables, methods and code blocks only need to be modified with static, that is, they are "ready" when the class is loaded That is, it can be used or has been executed. Can be executed away from the object. Conversely, if there is no static, it must be accessed through an object.

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
分享
二维码
< <上一篇
下一篇>>