When do you define static members? Or, when defining a member, does it need to be statically decorated? There are two types of members: 1, member variables. (data sharing is static)
Whether the data for this member variable is the same for all objects:
If so, then the variable needs to be statically decorated because it is a shared data. If not, then say that this is the object's unique data to be stored in the object. 2, member function. (defined as static when no unique data is called in the method) what if the member function needs to be statically decorated?
As long as the reference is made, the unique data in the object is accessed within the function: if there is access to unique data, the method cannot be statically decorated.
If no unique data has been accessed, then this method needs to be statically modified.
The difference between a member variable and a static variable:
1, the member variable belongs to the object. So also known as instance variables.
Static variables belong to the class. So it is also called a class variable. 2, the member variable exists in heap memory.
Static variables exist in the method area.
3, member variables exist with the creation of the object. Disappears as the object is recycled.
Static variables exist as classes are loaded. Disappears as the class disappears. 4, member variables can only be called by the object.
A static variable can be called by an object or by a class name.
Therefore, member variables can be called unique data for objects, and static variables are called shared data for objects.
Static Note: The static life cycle is very long.
Static code block: a code block area with a static keyword. defined in the class.
Java Knowledge Point 7