Learn the basic knowledge step by step:
Intuitively: static modifiers are available for static methods, but not for non-static methods.
I. Differences between static and non-static members
- Static members belong to the class, rather than static members belong to the class instance.
- Non-static members: each time a class instance is created, a storage space is opened for non-static members in the memory.
- Static members: no matter how many class instances are created, the static members of the class only occupy the same region in the memory.
Ii. Differences between static and non-static methods
- Static Methods belong to the class, rather than static methods belong to the class instance.
- A static method can only be a static member of the primary class, rather than a static method can be any member of the primary class.
- Static methods can be used before instantiation. The non-static variables in the class must be instantiated before memory can be allocated.
C # The memory address used by non-static variables cannot be determined during static method calls. So it cannot be used. The static variable address is fixed for the class, so it can be used.
- Static methods are more efficient than instantiation. The disadvantage of static methods is that they cannot be destroyed automatically, while instantiated methods can be destroyed.
- The static method and static variable always use the same memory after being created, and multiple memories will be created using the instance method.
- The tatic method is a member method in the class and belongs to the entire class, that is, it can be called directly without creating any object!
Only static variables and other static methods can appear in static! In addition, the keyword such as this... cannot be used in the static method because it belongs to the entire class!
- A static method is a special member method. It does not belong to a specific instance of the class, but to the class itself. Therefore, you do not need to create a class instance for the static method, but use the class name. Static Method format.