Static keywords, which must be learned by java, are hard to understand when many friends write and read code. The following describes how to learn static keywords.
I. static keywords
The original member variables in a class, each new object, have their own member variables, because these member variables are not static member variables. For static member variables, this member variable has only one copy, and this copy is shared by all objects in this class.
1. Differences between static and non-static member variables
The following example shows
Package cn. galc. test; public class Cat {/*** static member variable */private static int sid = 0; private String name; int id; Cat (String name) {this. name = name; id = sid ++;} public void info () {System. out. println ("My Name is" + name + ", NO. "+ id);} public static void main (String [] args) {Cat. sid = 100; Cat mimi = new Cat ("mimi"); Cat pipi = new Cat ("pipi"); mimi.info (); pipi.info ();}}
Draw a memory analysis graph to understand the execution process of the entire program.
The first sentence of the execution program: Cat. sid = 100; the sid here is a static member variable, which is stored in the data zone (data seg). Therefore, first allocate a small space sid in the data zone, after the first statement is executed, the sid contains a value of 100.
The memory layout is as follows:
Next, the program runs:
Cat mimi = new Cat ("mimi ");
Here, call the Cat class constructor Cat (String name). The constructor is defined as follows:
Cat (String name ){
This. name = name;
Id = sid ++;
}
First, allocate a small memory mm in the stack memory, which contains the address of the Cat class instance object in the heap memory, mm is the reference object of Cat class objects in heap memory. This constructor declares a string-type parameter variable. Therefore, "mimi" is passed to the constructor as a real parameter. Because string constants are stored in the data zone, therefore, a small memory block is added to the data zone to store the string "mimi ". The memory distribution is as follows:
When calling the constructor, first allocate a small space for the parameter name in the stack memory. The name is name, and then the string "mimi" is passed to the name as a real parameter, A string is also a reference type. Except for the four types of basic data, all other types are reference types. Therefore, strings can be considered as an object. So here it is equivalent to passing the reference of the "mimi" object to the name, so now the name points to "mimi ". The memory layout is as follows:
Next, execute the code in the constructor body:
This. name = name;
Here this refers to the current object and the cat in the heap memory. Here, the value in the name in the stack is passed to the name attribute of the cat object in the heap memory, therefore, the value in this name can also be found in the data area of the string object "mimi", this name is also a reference object of the string object "mimi, with its property value, you can find the string object "mimi" located in the data area ". The memory distribution is as follows:
Next, execute another code in the method body:Id = sid ++;
Here, the sid value is passed to the id, so the id value is 100. After the sid is passed, add another 1, and the sid is changed to 101. The memory layout is shown in.
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 for the name in the stack space will disappear. The reference of the string "mimi" pointing to the data area in the stack memory also disappears, and only the reference pointing to the string "mimi" in the heap memory does not disappear. The memory layout is as follows:
Run the following command:Cat pipi = new Cat ("pipi ");
Here is the second call constructor Cat (). The entire call process is the same as the first call. After the call is completed, the memory layout is shown in:
The last two sentences of code are printed by calling the info () method. The result is as follows:
Through this program, we can see the role of the static member variable sid, which can be counted. Every time a new cat comes out, it is recorded as a number. Let it add 1 on its own.
After the program is executed, the entire layout in the memory is shown in. It continues until the moment before the completion of the main method call.
Here, we call the constructor Cat (String name) to create two cats. First, we allocate two blocks of space, mimi and pipi, in the stack memory, respectively. The addresses of the two cats can be found, the mimi and pipi correspond to the references of two cats in the heap memory. Here, the constructor declares a variable of the string type, and the String constant is allocated in the data area. Therefore, both the passed strings mimi and pipi are stored in the data area. Therefore, the data zone is allocated with two small pieces of memory for the storage string mimi and pipi, containing the strings "mimi" and "pipi". The strings are also reference types, all other data types except the four categories and eight basic data types are reference types. Therefore, we can think that a string is also an object.
Here there are two new cats, both of which have their own id and name attributes, so the id and name here are both non-static member variables, that is, there is no static modification. So every time a new cat comes out, the new cat has its own id and name, that is, the id and name of non-static member variables are a separate copy of each object. But for static member variables, there is only one copy, no matter how many new objects, even if not new objects, static member variables will be retained in the Data zone. Like the sid here, the sid is stored in the data zone. No matter how many new cats are stored in the heap memory, the sid has only one copy, and only one copy is retained in the Data zone.
Static member variables belong to the entire class and do not belong to a specific object. So how to access the value of this static member variable? First, the static value can be accessed by any object, and the same memory is accessed during access. Second, you can access this static value even if there is no object. the static member variable name is used to access this static value. "In addition, there is something behind it, so the things behind it must be static, such as" System. out ", which is added by the class name (System class). to access this out, so this out must be static.
Let's look at the following code.
Package cn. galc. test; public class Cat {/*** the sid here is no longer a static member variable, because there is no static modifier. * In this case, it is a common non-static member variable in the class, like id and name, * is an attribute of every new object. */Private int sid = 0; private String name; int id; Cat (String name) {this. name = name; id = sid ++;} public void info () {System. out. println ("My Name is" + name + ", NO. "+ id);} public static void main (String [] args) {// Cat. sid = 100; the class cannot be used here. the static member variable is used to access the sid, because the sid is now a non-static member variable. Therefore, you must comment out this sentence; otherwise, it cannot be compiled. Cat mimi = new Cat ("mimi"); Cat pipi = new Cat ("pipi"); mimi.info (); pipi.info ();}}
The only difference between this code and the previous Code is that the static modifier for declaring the sid variable is removed, and the sid is no longer a static member variable, but a non-static member variable, at this time, each new cat object will have its own unique sid attribute. After this code is executed, the memory layout is shown in:
Because sid becomes a non-static member variable, the counting function is no longer available. Like the attributes of id and name, sid is an attribute of each new object. Therefore, a new cat is added with a sid attribute. The class name cannot be used any more. access sid in the format of static member object name, so the first sentence of the code is "Cat. sid = 100; "cannot be used in this way; otherwise, an error occurs during compilation. You must comment out this sentence before compilation is successful. Since the sid value cannot be accessed, the sid value is always the value 0 assigned during initialization. When the constructor is called and the code id = sid ++ in the method body is executed, sid first assigns its own value 0 to id, so the value of id is 0, then sid adds 1, so sid becomes 1.
The difference between static variables and non-static variables is that static variables can be used for counting, but not static variables.
After understanding the memory, you can understand everything and various languages. All languages are like this: the memory allocated by local variables is always in the stack, the memory allocated by new things is always in the heap, and the memory allocated by static things is always in the Data zone. The remaining code must be in the code area. All languages are like this.
In a static method, if you want to access a non-static member variable, you cannot directly access it. You must create an object in the static method to access it. If the static member variable is added, the member variable is a static member variable, which can be directly accessed in the main method.
The main method is a static method. To execute the main method, you do not need to create a new object.
Dynamic methods are called for an object. Static methods are not called for an object and can be used without objects. Therefore, you can call static methods in the form of "classname. method. Therefore, it is not possible to access non-static member variables in the main method. It is also not possible to access non-static methods in the main method, because non-static methods can only be called for an object, without an object, the executor of the method cannot be found.
The member variable allocates storage space in the heap memory only when a new object is generated. Local variables allocate storage space in the stack memory.
Static methods are no longer called for an object, so they cannot access non-static members.
A non-static member belongs to a specific object. to access a non-static member, you must create an object to access it.
Static variables can be accessed through the object name or through the class name. Both of them access the same memory.
The above is all the content of this article. The amount of information is large and you need to read it patiently to truly learn the java static keyword.