java keyword-static

Features:

1. You want to achieve object sharing of common data in objects. This data can be statically modified.

2. The statically modified members can be called directly by the class name. In other words, there is one more way to call static members. classname.Static method.

3. Static is loaded as the class is loaded. And it takes precedence over object existence.

Disadvantages:

1. Some data are object-specific data and cannot be statically modified. Because then, the unique data becomes the shared data of the object. That's the problem with the description of things. Therefore, when defining static, it must be clear whether this data is shared by objects. 2. Static methods can only access static members, not non-static members. Because the static method is loaded before the object exists, there is no way to access the members in the object.

3. The keyword this and super cannot be used in static methods. Because this represents an object, and when static is present, there may be no object, so this cannot be used.

4. The main function is static. When should static members be defined? In other words: when defining members, do they need to be statically modified? There are two types of members:
1. Member variables.

Whether the data of the member variable is the same for all objects: If so, then the variable needs to be statically modified because it is shared data. If not, then it is said that this is the specific data of the object and should be stored in the object.

2, member functions.

 Whether the unique data in the object is accessed in the function: If there is access to the unique data, the method cannot be statically modified. If no specific data has been accessed, then this method needs to be statically decorated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325882731&siteId=291194637