Objective
Static properties and methods must use the static modifier
The difference between static and non-static properties:
1, in memory location different all properties with static modifier or methods are stored in the memory of the method area rather than the static property in the memory of the heap area
2. When there is a different static property or method exists before the object is created and not a static property exists after the object is created
3. A static property is shared among all objects in a class
4. Life cycle different static properties are destroyed after the class disappears and the non-crystalline attribute is destroyed after the object is destroyed
5. Usage:
A. Static properties can directly access non-static properties through the class name cannot be accessed directly through the class only through object access
B. The same point is that both can be used after the object is created to use the following to see a static property is a shared example of all objects in a class The final result is 20.
Class Beauty{int age1;static int age2;} Class Testperson{public static void Main (string[] args) {Beauty B1 = new Beauty (); b1.age2 = 10; Beauty b2 = new Beauty (); b2.age2 = 20; System.out.println (B1.age2); System.out.println (B2.age2); System.out.print ("\ n");}}
Static properties in-memory distribution map:
Considerations for using static:
1. Methods with static modifiers can only access static properties
2. Non-static methods that can access static properties can also access non-static properties
3. Static variables cannot be defined by non-static methods
4. The This keyword cannot be used in static methods
5. Static methods cannot invoke non-static methods, but can
static properties and static methods of Java