Let's look at the following code
PackageJavastudy; Public classStaticdemo { Public Static voidMain (string[] args) {Child C1=NewChild (); C1.name= "Zhangsan"; C1.country= "CN";//Code DuplicationC1.speak (); Child C2=NewChild (); C2.name= "Lisi"; C2.country= "CN";//Code DuplicationC2.speak (); }}classchild{String name; String Country; voidspeak () {System.out.println ("My name is" +name+ ", my country is" +country); }}
In the code above, C1 and C2 two objects, including an identical property value ". country=" CN ", this attribute and attribute value are common to two objects.
The code repeats, and if the object is incremented, this property needs to be written again.
So, is there any way to extract this common attribute value, store it separately, and have access to it altogether?
At this point, use the keyword-static
Static: Used to decorate members (member variables, member methods).
The code after using static is as follows:
PackageJavastudy; Public classStaticdemo { Public Static voidMain (string[] args) {Child C1=NewChild (); C1.name= "Zhangsan"; C1.speak (); Child C2=NewChild (); C2.name= "Lisi"; C2.speak (); }}classchild{String name; StaticString country= "CN"; voidspeak () {System.out.println ("My name is" +name+ ", my country is" +country); }}
Operation Result:
My name is Zhangsan,my country is CN
My name is Lisi,my country is CN
Static usage:
1. For modifying members (member variables and member methods)
2, when the member is statically modified, except can be called by the object, can be called directly by the class name-class name. Static members.
Static characteristics:
1, static load with the load of the class.
As in the example code above, the child class instance is created, and the class is loaded into memory, and country= "CN" opens up space in memory.
This means that the static disappears as the class disappears. indicating the longest life cycle. When the object is exhausted, it disappears.
2, precedence over the existence of the object. static First, the object is back-saved .
3. Shared by all objects.
4, can be directly called by the class name.
The difference between a class variable and a member variable:
1, storage location.
Class variables are present in the method area as the class is loaded, and member variables exist in heap memory as the object is built.
Content that is modified by static is not in heap memory, but in a separate method area (also known as a shared or data region). Unique objects are stored in heap memory as objects.
2. Life cycle
The class variable has the longest life cycle and disappears with the disappearance of the class, and the member variable disappears as the object disappears.
Static loss of Use precautions:
1. Static methods can only access static members. Non-static methods can access both static and non-static members.
1, the static method can not define this, super, because static precedence over the existence of the object.
Static pros and Cons:
Benefit: Store the shared data of the object in a separate space, save memory, and save one copy of each object.
Can be called directly by the class name.
Cons: The life cycle is too long.
Access is limited and can only be accessed statically.
Example 1,
PackageJavastudy; Public classStaticdemo { Public Static voidMain (string[] args) {System.out.println (child.country); //The object is not created and can be called directly with the class name. exists before the object. }}classchild{String name; //This is a member variable, also known as an instance variable. StaticString country= "CN";//This is a static member variable, also known as a class variable. voidspeak () {System.out.println ("My name is" +name+ ", my country is" +country); }}
Output: CN
No objects are created, but class variables can be called with the class name. Static overrides the existence of the object.
Summarize:
Whether the data value is shared by multiple objects is used statically, not static.
When called with the class name, it is modified with static.
The Static keyword for Java