- If some operations do not depend on a specific instance, they are static. If some operations depend on a specific instance (for example, accessing a specific member name), they should be instantiated.
-
- Static methods can be called directly without a new object.
-
- 1. class-related and Object-independent
2. The "light" method of the object is not required.
3. Factory method
-
- If a method is frequently used, or the method itself has a strong versatility and does not need to initialize class member variables, you can use static methods, which is convenient and fast.
-
- It can be used directly, even if it is static.
-
- No specific object is involved, because no non-static members can be directly used in static methods.
-
- (1) Production Tools
(2) It can be used as an "Bureau" object or method.
-
- The static method is the same as the instance method, and is loaded when the type is used for the first time. There is basically no difference in the call speed.
-
- Static methods can be called without instance creation. It is relatively simple from the object-oriented perspective, when you choose to use the instantiation method or static method, the method should be based on whether the method has a logical correlation with the instantiated object. If so, the instantiated object should be used; otherwise, the static method should be used.
-
- The
Frequently Used
In the tool class (such as sqlhelper)
-
- The proper use of the static method itself is nothing. When a person never understands the use of Polymorphism or interface design, it is natural to abuse the static method.
-
- I personally understand that methods that need to be called in multiple classes and are irrelevant to objects can be set as static methods for convenient calls.
-
- Methods common to all objects
- No operations related to specific objects
For example, the student's age is related to the student.
Changing the student's age is not suitable for static methods.
In general, if you do not use the this keyword in your method,
It is suitable for static methods.
-
- Some common methods in common classes can be designed as static classes.
-
- As long as the status information of the class is not used, the information obtained only from the parameter can be regarded as static
-
- Some special design patterns can be implemented: such as Singleton
-
- Without this pointer, some system API callback functions can be encapsulated into the class in the form of static functions.
-
- SomeAlgorithmSuch as mathematical functions, such as ln, sin, tan, etc. These functions do not need to belong to any object, so it is better to call them in the class.
-
- In short, from the perspective of OOA/OOD, all functions with definite behavior patterns can be designed as static functions without instantiation.
-
- The most obvious difference between a static method and a non-static method is that if a method is public static, you can directly use the class name. the method of the method name is called, while the Public instance method needs to instantiate the object in advance before calling.
These arguments are basically correct.