Static and javastatic JAVA members
Static keywords
I. role: it is a modifier used to modify members (member variables, member methods)
1. Only one member variable is modified by static.
2. When a member is modified as static, an access method is provided. In addition to being called by an object, the member can also be called directly by the class name (class name. static member)
Ii. static features:
1. Loaded with the loading of Classes
2. takes precedence over the existence of Objects
3. shared by all objects
4. It can be called directly by the class name.
Iii. storage location:
1. static member attributes (class variables) exist in the data memory zone as the class is loaded.
2. Attributes of common members exist in heap memory as objects are created.
Iv. lifecycle:
1. Static members (class variables) have the longest life cycle and disappear as the class disappears
2. the life cycle of non-static members (instance variables) is shorter than that of static members, and disappears as the object disappears.
5. Method considerations:
1. Static methods can only access static members.
2. A non-static method can access static members (member attributes, member methods) and non-static members.
3. the keyword "this" and "super" cannot be defined in the method today. Because the static state takes precedence over the object existence, the static state method cannot contain the keyword "this ".
Class G {String name; int age; // member attribute (another name (instance variable) static String country; // static member attribute (another name: class variable) static void jt () {System. out. println (country); // when you access static member attributes in a static method, you cannot use this keyword or access non-static member attributes. } Public class Index {public static void main (String [] args) {g a = new G ();. country = "James"; g B = new G (); System. out. println (. country); // object A, which defines the value of the country member attribute and can be output. This is A conventional System. out. println (B. country); // object B, which does not define the value of the country member attribute. You can also output System. out. println (G. country); // class name. static member. You can also output B. jt (); // call the static method }}