Traditional Culture Encyclopedia - Traditional stories - What's the difference between a static method and a non-static method?
What's the difference between a static method and a non-static method?
The definitions of static class and non-static class in C# are basically the same, but the static class definition needs to add a static modifier.
Second, the life cycle:
Like static member variables, static methods belong to the class itself. When a class is loaded, it is loaded into memory and will remain in memory until the JVM is shut down.
Non-static methods, also called instantiation methods, belong to instance objects, which will allocate memory after instantiation, so they must be referenced by instances of classes. Without memory, when the instance object is recycled by JVM, it will disappear.
Third, efficiency:
Static methods are more effective than non-static methods.
According to the above concept, as long as there is enough memory, you can use static.
Fourth, thread safety:
The static method is * * * shared code segment, and the static variable is * * * shared data segment. Since it is "* * *", there is a competition problem.
Non-static methods are aimed at an object, so there will be no security problems in the line.
Static methods are the same as instance methods and are loaded when the type is used for the first time. There is basically no difference in call speed.
Comparison between static and non-static members of verb (verb's abbreviation);
1. Static members belong to classes, and non-static members belong to instances of classes.
2. Every time an instance of a class is created, a new storage space will be allocated for non-static members in memory; Non-static members belong to classes and are common to instances of each class. No matter how many instances of the class are created, the static members of the class only occupy the same area in memory.
Six, different uses:
1. Static methods belong to classes and can be used before instantiating classes.
2. Non-static methods can access any member in the class, while static methods can only access static members in the class.
3. Because static methods can be used before the class is instantiated, and non-static variables in the class must be instantiated before memory can be allocated, only static variables and other static methods can appear in static! Besides, keywords like this ... can't be used in static methods ... because they belong to the whole class!
4. Static methods are more effective than instantiation. The disadvantage of static methods is that they are not destroyed automatically, while instantiation can be destroyed.
5. Static methods and static variables always use the same memory after creation, while using instances will create multiple memories.
- Related articles
- What kind of tea does green tea include?
- The practice of leek flower
- What do seasonal vegetables with traditional fermentation process do?
- What is the azure nop feature? ,
- How thick paper do flyers usually use? What are the requirements for paper?
- Historical evolution of iron plow
- What is woodworking glue?
- What are the places worth visiting in Guangdong Province?
- An English introduction to the history, development and culture of hip-hop music.
- What song is that green hill?