Static keyword
One, function: is a modifier, used to decorate members (member variables, member methods)
1, after the static modified member variable only one copy
2, when the member is modified by static, a more access way, in addition to being called by the object can also be called directly by the class name (class name. Static members)
Second, the characteristics of static:
1, loaded with the load of the class
2. Precedence over the existence of objects
3. Shared by all objects
4, can be directly called by the class name
Three, storage location:
1. Static member properties (class variables) exist in the data memory area as the class loads.
2. Normal member properties exist in heap memory as the object is established.
Iv. Life cycle:
1, static members (class variables) the longest life cycle, with the disappearance of the class disappears
2. Non-static members (instance variables) have a shorter life cycle than static members and disappear as the object disappears
Five, the method of attention:
1. Static methods can only access static members
2. Non-static methods that can access static members (member properties, member methods) can also access non-static members
3, today method is not to define the this, super keyword, because static precedence over the existence of the object, so the static method can not appear this
classg{String name; intAge//member properties (another term (instance variable) StaticString Country;//Static member properties (another term: class variables) Static voidJT () {System.out.println (country); //The This keyword cannot be used when accessing static member properties inside a static method, and non-static member properties cannot be accessed. }} Public classindex{ Public Static voidMain (string[] args) {G A=NewG (); A.country= "Xiao Ming"; G B=NewG (); System.out.println (A.country); //object A, which defines the value of the country member property, can be output, which is the general waySystem.out.println (B.country);//object B, which does not define the value of the country member property, can also outputSystem.out.println (G.country);//the class name, static member, can also outputB.JT (); //calling a static method }}
JAVA Static member Static