- If some operations do not depend on a specific instance, then it is static, whereas if something is dependent on a specific instance (such as accessing the name of a particular member), it should be instantiated.
- Static methods can be called directly without a new object
- 1. Related to class and object-Independent
2. "Light" method that does not require an object
3. Factory method
- If a method is used at a higher frequency, or the method itself is more versatile, without initializing a class member variable, you can use a static method, which is convenient and fast.
- You can use the method directly, even if it is static.
- No specific object is involved, because within a static method, any non-static member cannot be used directly.
- (1) Production tool class
(2) may be used as a "bureau" object or method
- Static methods and instance methods are the same, loaded when the type is first used. The speed of the call is basically no different.
- Static methods can be called without creating an instance, and relatively simple from an object-oriented perspective, when you choose to use an instantiation or static method, you should use an instantiated object instead of a static method, depending on whether the method and the instantiated object have a logical dependency.
- You do not need to generate an object
Often used frequently.
In a tool class (e.g. SqlHelper)
- Proper use of the static method itself is nothing, and when a person never understands the use of polymorphic, interface design, it is natural to misuse the static method.
- The individual understands that calls are required in multiple classes and that object-independent methods can be set to static methods for easy invocation.
- Methods common to all objects
- No longer related to any particular object-specific operations
For example, the student's age is the student's correlation.
Modifying a student's age is not appropriate for a static method.
In general, if you do not use the This keyword in your method,
That's a good way to use static methods
- Some common methods commonly used in common classes can be designed as static classes
- As long as the state information of the class is not used, only the information obtained from the parameter can be static.
- Some special design patterns can be implemented: such as Singleton
- Because there is no this pointer, the callback functions of some system APIs can be encapsulated in the form of a static function to the inside of the class.
- Some algorithms can be encapsulated, such as mathematical functions, such as Ln,sin,tan and so on, and these functions do not necessarily belong to any one object, so invoking from a class feels better
- In short, from the perspective of ooa/ood, everything that does not need to be instantiated can have a function that determines how the behavior should be designed to be static
- The most obvious difference between static and non-static methods is that if a method is public static, it can be called directly from the class name method, and the public instance method needs to instantiate the object beforehand before invoking it.
These various statements are basically correct.
When static methods are used