The difference between static and non-static methods:
1. Static methods can be called without class instantiation, whereas non-static methods need to be instantiated before they are invoked;
2. Static methods can only access static members and methods, non-static methods are accessible;
3. Static methods cannot be marked as override, resulting in a derived class that cannot be overridden, but can be accessed;
4. Static members are initialized the first time they are used. Non-static members are in the creation of objects, from the memory allocation of static is continuous, non-static in memory is discrete, so static methods and non-static methods, at the call speed, static method speed must be fast, because non-static methods need to instantiate, allocate memory, but static methods do not, But this difference in speed can be negligible.
When to use a static method, when to use an instance method:
1. A method is independent of the instance object of his class and does not depend on the new instance, then it should be static
2. Do not need the object-oriented concept when using static, such as the animal-run, because you may expand the cat run, dog run, this time you need to use a non-static method
About thread Safety:
Static methods have only one instance, when there are static variables, for high concurrency, all concurrent execution go that way, the value of static variables will change, resulting in problems;
Of course, concurrency is not safe with the method itself, and the shared resource has a relationship such as a variable, each thread to operate on it, can change its value, multithreading (concurrency is actually multithreaded), operation it, its value confusion, there is a problem
Why use non-static methods
This is I write a method, originally directly written static method, no other reason, because do not want to instantiate can be less one line of code, and did not think of static and non-static difference = =! Of course it is possible to write a non-static method;
Reason
1. Because the method requires repeated calls, each time the new one will cause memory overhead, the static method will always be in memory, do not need to re-new a piece of space to initialize the data;
2. Do not need to do oo;
3. The method does not rely on new content;
Compared to the classic static method, Dbhelp is basically a static method