|
Understand the memory, understand everything, understand a variety of languages. All the languages are like this: The local variable allocation memory is always inside the stack, new out of the allocated memory is always in the heap, static things allocated memory is always in the data area. The rest of the code must be in the code area. This is true of all languages. |
1
A member variable that uses static declarations in a class is a static member variable, which is a common variable for the class that is initialized the first time it is used.
The static member variable has only one copy for all objects of the class.
2
A static method is declared with static, and no reference to the object is passed to it when the method is called, so non-static members are inaccessible in the static method
(Static static method is no longer a call to an object, so non-static members cannot be accessed)
Static members can be accessed through object references or class names (no instantiation required)
The member variables in the original class, each new object, has a member variable of its own, because these member variables are not static member variables. For static member variables, there is only one copy of this member variable, and this is a share of all objects in the class.
The member variables in the original class, each new object, has a member variable of its own, because these member variables are not static member variables. For static member variables, there is only one copy of this member variable, and this is a share of all objects in the class.
1.1. The difference between a static member variable and a non-static member variable
Take the following example to illustrate
1 package cn.galc.test; 2 3 public class Cat {4 5 /** 6 * Static member Variable 7 */ 8 private static int sid = 0; 9 private Stri ng name;11 int id;13 Cat (String name) { this.name = name;16 id = sid++;17 }18 c void info () { System.out.println ("My name is" + name + ", NO." + ID);}22 the public static void Main ( String[] args) { cat.sid = 100;25 cat Mimi = new Cat ("Mimi"), + cat pipi = new Cat ("Pipi"); mimi.i NFO (); pipi.info (); }30}
Understand the entire program's execution process by drawing a memory analysis diagram
The first sentence of the execution program: CAT.SID = 100; When the SID here is a static member variable, the static variable is stored in the data seg, so a small space SID is allocated in the data area first, and after the first sentence is executed, the SID contains a value of 100.
The memory layout at this point is as follows
Then the program executes to:
Cat Mimi = new Cat ("Mimi");
Here, the construction method of the cat class is called Cat (String name), and the constructor method is defined as follows:
Cat (String name) {
THIS.name = name;
id=sid++;
}
The call first allocates a small chunk of memory in the stack memory mm, which contains the address of the instance object that can find the cat class inside the heap memory, MM is the reference object of the Cat class object inside the heap memory. This constructor declares a parameter variable of type string, so "Mimi" is passed to the constructor as an argument, because the string constant is allocated in the data area , so there is a small chunk of memory in the data area to store the string "Mimi". The memory distribution at this point is as follows:
When the constructor method is called, the parameter name is assigned a small space in the stack memory, called name, and then the string "Mimi" is passed as an argument to name, and the string is a reference type, except for the four classes of 8 basic data types, all of which are reference types. So you can assume that a string is also an object. So here's the equivalent of passing a reference to the "Mimi" object to name, so now name is pointing to "Mimi". So the layout of the memory at this point is as follows:
Then execute the code inside the constructor body:
This.name=name;
This refers to the current object, which refers to the cat inside the heap memory. Here, the value contained in the name inside the stack is passed to the Name property of the Cat object in the heap memory, so the value contained in this name can also find the string object "Mimi" in the data area, and this name is also a reference object of the String object "Mimi". , the String Object "Mimi" in the data area can be found by its property value. The memory distribution at this point is as follows:
Next executes another code in the method body:
id=sid++;
Here is the value of the SID is passed to the ID, so the value of the ID is 100,sid after the pass, and then add 1, when the SID becomes 101. The memory layout at this point is as shown.
In this way, the construction method call is complete, and the memory space occupied by the local variables allocated to the constructor is all gone, so name in the stack space disappears. The reference to the string object "Mimi" inside the data area inside the stack memory disappears, leaving only the reference to the string object "Mimi" in the heap memory to disappear. The memory layout at this point is as follows:
Next execution: Cat Pipi = new Cat ("Pipi");
Here is the second call to construct the method cat (), the entire calling process is the same as the first time, after the call ends, the memory layout is as follows:
The last two lines of code are called the info () method to print out the results as follows:
Through this program, it is possible to see the function of this static member variable SID, which can be counted. Whenever a cat new comes out, give it a count. Add 1 to it yourself.
When the program finishes executing, the entire layout in memory is as shown. Continues until the main method call completes the first moment.
Call the construction method here Cat (String name) to create two cats, first in the stack memory allocated two small space Mimi and Pipi, which can find the address of the two cats, respectively, Mimi and pipi corresponding to the heap memory of two cat references. The constructor here declares a variable of type string, and the string constant is allocated in the data area, so this will store the passed string Mimi and Pipi in the data area. So there are two small chunks of memory in the data area that are stored in the string Mimi and Pipi, with the string "Mimi" and "Pipi", and the string being the reference type, except for the underlying data type of the four classes, all other data types are reference types. So you can assume that a string is also an object.
Here is new two cats out, the two cats have their own ID and name attribute, so here the ID and name are non-static member variables, that is, no static decoration. So every new cat, this new cat has its own ID and name, that is, the non-static member variable ID and name is a separate copy of each object. However, for static member variables, only one copy, regardless of how many objects new, even if the new object, the static member variable in the data area will retain a copy. Like the SID here, the SID is stored in the data area, and no matter how many cats are in the heap memory, the SID has only one copy in the data area.
A static member variable belongs to the entire class, and it does not belong to a specific object. So how do you access the value of this static member variable? First 1th, any object can access this static value, access is the same piece of memory. 2nd, even if there is no object can access this static value, through the "class name. Static member variable name" To access this static value, so later see a class name plus "." Plus there is a thing behind, then this thing must be static, such as "System.out", here is the class name (System Class) plus "." To access this out, so this out must be static.
And look at the code below.
1 package cn.galc.test; 2 3 public class Cat {4 5 /** 6 * The SID in this is no longer a static member variable, because there is no statics modifier, 7 * At this point it is a normal non-static member variable inside the class, and the ID, As name, 8 * becomes a property of each new object. 9 */10 private int sid = 0;11 -Private String name;13 int id;15 -Cat (string name) {17
this.name = name;18 id = sid++;19 }20 public void info () {$ System.out.println ("My name is" + Name + ", NO." + ID); }24 public static void Main (string[] args) { //cat.sid = 100; You can no longer use the class. Static member Variable Format to access the SID, because the SID now becomes a non-static member variable. So you have to comment out this sentence, or you cannot compile the pass. cat Mimi = new Cat ("Mimi"), cat pipi = new Cat ("Pipi"), mimi.info (); }32}
The only difference between this code and the previous code is that the static modifier of the declaring SID variable is removed, and the SID is no longer a static member variable, but a non-static member variable, and each new cat object will have its own individual SID attribute. So after this code execution is complete, the in-memory layout looks like this:
Since the SID becomes a non-static member variable, there is no longer a count function. The SID, like the ID and Name properties, becomes the property of each new object, so each new cat comes with a SID attribute. The first sentence of the code is "CAT.SID = 100;" Because the SID cannot be accessed by using the "class name. Static member object name" format. This cannot be used, otherwise the compilation will go wrong and the sentence must be commented out in order to compile successfully. Since the value of the SID can not be accessed, the value of the SID has always been the value assigned to it at initialization 0. Until the constructor method is called, the code that executes into the body of the method id=sid++, the SID first assigns its own value 0 to the ID, so the ID value is 0, and then the SID adds 1, so the SID becomes 1.
So the difference between a static variable and a non-static variable is that a static variable can be used to count, not a static variable.
Understand the memory, understand everything, understand a variety of languages. All the languages are like this: The local variable allocation memory is always inside the stack, new out of the allocated memory is always in the heap, static things allocated memory is always in the data area. The rest of the code must be in the code area. This is true of all languages.
In a static method, if you want to access a non-static member variable, it is not directly accessible, it must be in a static method of the new object to be accessed. If the static member variable is added, then this member variable is a static member variable and can be accessed directly within the main method.
The main method is a static method, and the main method does not need to be a new object when it is executed.
A dynamic method is called on an object, and a static method is not invoked on an object, and no object can be used. You can use the form of "Classname.method ()" To invoke a static method. Therefore, it is not possible to access non-static member variables in the main method, it is not possible to access the non-static method inside the main method, because the non-static method can only be called for an object, there is no object, can not find the executor of the method.
Member variables allocate storage space in the heap memory only when new objects come out. Local variables allocate storage space inside the stack memory.
Static methods are no longer called on an object, so non-static members cannot be accessed.
A non-static member is dedicated to an object, and a non-static member must have a new object in order to access it.
Static variables can be accessed through the object name or by the class name, both of which are accessed by the same piece of memory.
"Static variables can be accessed through the object name or through the class name, both of which are accessed in the same piece of memory." ", it is best to emphasize that although you can access the static variables and methods through the object name, but at the bottom or the class name to access the call, so in programming, it is best not to use the object name to access static variables and methods, which is actually a flaw in Java design.
Static keyword (i)