One, static key word
The original member variable in a class, each new object, this object has a member variable of its own, because these member variables are not static member variables. For a static member variable, this member variable has only one copy, and this one is shared by all objects in this class.
1.1. The difference between a static member variable and a non-static member variable
Take the following example to illustrate
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 ("I name is" + name + ", NO." + ID);
}
public static void Main (string[] args) {
cat.sid = m;
Cat Mimi = new Cat ("Mimi");
Cat Pipi = new Cat ("Pipi");
Mimi.info ();
Pipi.info ();
}
Understand the execution of the entire program by drawing a Memory analysis diagram
The first sentence of the execution procedure: cat.sid = 100, when the SID here is a static member variable, the static variable is stored in the data area (information seg), so first allocate a small space SID inside the data area, after the first sentence is executed, the SID contains a value of 100.
The diagram of the memory layout at this point is shown below
The program then executes to:
Cat Mimi = new Cat ("Mimi");
Here, we call the construction method Cat (String name) of the Cat class, which is defined as follows:
Cat (String name) {
THIS.name = name;
id=sid++;
}
Call the first in the stack memory allocated a small block of memory, which can be found in the heap memory inside the Cat class instance object address, mm is heap memory inside the Cat class object reference object. This constructor declares a parameter variable of the string type, so here "Mimi" is passed into the constructor as an argument, and because the string constants are stored in the data area, a small chunk of memory is stored in the data area to store the string "Mimi". The memory distribution at this point is shown in the following illustration:
When the constructor method is invoked, the first is to assign a small chunk of space to the parameter name in the stack memory, named name, and then pass the string "Mimi" as the argument to name, and the string is a reference type, except for the four classes of 8 underlying data types, all of which are reference types, So you can think of a string as an object. So here's the equivalent of passing the reference to "Mimi" to name, so now name is pointing to "Mimi." So the layout of the memory at this point is shown in the following illustration:
Next, execute the code inside the constructor method body:
This.name=name;
This here refers to the current object, which refers to the cat in the heap memory. Here the value contained inside the stack is passed to the name attribute of the Cat object in the heap memory, so the value contained in the name can also find the string object "Mimi" in the data area, at which point the name is also a reference object of the String object "Mimi" , the String Object "Mimi" located in the data area can be found by its property value. The memory distribution at this point is shown in the following illustration:
Next executes another code in the body of the method:id=sid++;
Here is the value of the SID passed to the ID, so the value of the ID is 100,sid after passing, add 1 to yourself, at this time the SID becomes 101. The memory layout at this point is shown in the following illustration.
In this way, the constructor call completes, and the memory space of the local variable assigned to the constructor disappears, so the name in the stack space disappears. The reference to the string object "Mimi" inside the stack memory pointing to the data area disappears, and the reference to the string object "Mimi" in the heap memory remains. The memory layout at this point is shown in the following illustration:
Next execute:cat pipi = new Cat ("Pipi");
Here is the second call to construct Method Cat (), the entire call procedure is the same as the first time, after the call is finished, the memory layout is shown in the following illustration:
The last two lines of code are called the info () method to print out, the results are as follows:
Through this program, you can see the role of this static member variable SID, which counts. Whenever a cat new comes out, remember to give it a number. Let it add up to 1 on its own.
When the program is finished, the entire layout in memory is shown in the image above. Continues until the first moment the main method call completes.
This calls construct method Cat (String name) to create two cats, first in the stack memory allocated two small pieces of space Mimi and Pipi, which can find the address of the two cats, Mimi and pipi corresponding to the heap of memory two cat reference. The constructor here declares a variable with a string type, and the string constants are assigned to the data area, so the string Mimi and Pipi are stored in the data area. So there are two small chunks of memory in the data area that contain string Mimi and Pipi, with strings "Mimi" and "Pipi", and strings that are reference types, except for the four classes of 8 underlying data types, all other data types are reference types. So you can think of a string as an object.
This is the new two cats out, all two cats have their own ID and name attribute, so here's ID and name are non-static member variables, that is, there is no static modification. 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. But for static member variables, only one, no matter how many new objects, even if not new objects, static member variables in the data area will also retain a copy. Like the SID here, the SID is stored in the data area, no matter how many cats are out of the heap, the SID has only one copy and only one 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 to the same 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's a thing behind it, then this thing must be static, like "System.out," and here's the class name (System Class) plus "." To access this out, so this out must be static.
And look at the code below.
Package cn.galc.test;
The SID in public class Cat {
/**
* is no longer a static member variable because there is no static modifier,
* At this point it is a normal non-static member variable in the class, like Id,name,
* Become the attributes that each new object has.
*
private int sid = 0;
private String name;
int id;
Cat (String name) {
this.name = name;
id = sid++;
}
public void info () {
System.out.println ("I name is" + name + ", NO." + ID);
}
public static void Main (string[] args) {
//cat.sid = 100; You can no longer use the format of class. Static member variables to access the SID because the SID now becomes a non-static member variable. So you have to comment out this sentence, or you can't compile it.
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 to remove the static modifier that declares the SID variable, at which point the SID is no longer a static member variable, but a non-static member variable, at which point each new cat object will have its own individual SID attribute. So after this code is done, the layout in memory is shown in the following illustration:
Because the SID becomes a non-static member variable, there is no longer a function of counting. The SID, like the ID and name attribute, is the property of each new object, so each new cat adds a SID attribute. Because the "class name. Static member Object name" format can no longer be accessed by the SID, the first sentence of the code "CAT.SID = 100;" Can not use this, or the compilation will be wrong, you must comment out this sentence to compile successfully. Since there is no access to the value of the SID, the value of the SID is always assigned a value of 0 when initialized. When the code id=sid++ is executed to the body of the method when the constructor method is invoked, the SID first assigns its own value of 0 to the ID, so the value of the ID is 0, and 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.
Understanding the memory, you understand everything, you understand a variety of languages. All languages are like this: local variables allocate memory forever in the stack, new out of the things allocated memory is always in the heap, static things allocated memory is always in the data area. The rest of the code is definitely in the code area. All languages are the same.
In a static method, if you want to access a non-static member variable, you cannot access it directly, you must have a new object in the static method to access it. If you add a static member variable, the member variable is a static member variable and can be accessed directly in the main method.
The main method is a static method, and the main method does not need a new object to execute.
Dynamic methods are invoked against an object, and static methods are not invoked against an object, and no object can be used. You can use the form "Classname.method ()" To invoke the static method. Therefore, it is not possible to access non-static member variables in the main method, and it is not possible to access non-static methods inside the main method, because Non-static methods can only be invoked against an object, and without objects, the performer of the method cannot be found.
Member variables allocate storage space in the heap memory only when new objects come in. Local variables allocate storage space in stack memory.
Static methods are no longer invoked against an object, so non-static members cannot be accessed.
Non-static members are exclusive to one object, and you must new an object to access a Non-static member.
Static variables can be accessed by object names or by class names, both of which are accessed in the same memory.
The above is the entire content of this article, the information is very large, need everyone to read patiently, so that the real learning Java static keyword.