Difference between static method and non-static method

Difference between static method and non-static method

Reprinted from: http://joe5456536.blog.163.com/blog/static/8537477320117136658629/

http://www.cnblogs.com/devinzhang/archive/2012/04/27/2473570.html

 

I know the use of static variable is to be careful as it is shared in every object. But my knowledge of static methods is limited to the following:

Lifecycle:

Static methods , like static member variables, belong to the class itself. They are loaded into memory when the class is loaded. They are not automatically destroyed and will remain in memory until the JVM is closed.

Non-Static Method ( Non-Static Method) is also called instantiation method. It belongs to instance object. Memory is allocated after instantiation and must be referenced through an instance of the class. It does not reside in memory, and when the instance object is reclaimed by the JVM, it also disappears. 

 ● Efficiency

 Static methods can be used more efficiently than non-static methods.

 

According to the above concept, as long as there is enough memory, static can be used. What are the problems with using static methods?

Thread safety

Static methods are shared code segments and static variables are shared data segments. Since it is "shared", there is a problem of concurrence.

Non-static methods are for a certain object, so there is no thread safety problem.

Static methods are the same as instance methods, loaded when the type is first used. There is basically no difference in the speed of the calls.

 

  • In frequently used tool classes that do not need to generate objects (such as SqlHelper)

  • Appropriate use of static methods is nothing in itself, when one never understands the use of polymorphism, interface design, it is natural to abuse static methods.

  • Personally understand that methods that need to be called in multiple classes and are not related to objects can be set as static methods for easy invocation.

  • Methods common to all objects

  • It is not related to any operations related to a specific object such as the age of the student is related to the student. Modifying a student's age is not suitable for static methods. Generally speaking, if your method does not use this keyword, it is suitable to use static method

  • Usually some commonly used methods 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 parameters can be static

  • Some special design patterns can be implemented: such as Singleton

  • Since there is no this pointer, the callback functions of some system APIs can be encapsulated inside the class in the form of static functions

  • Some algorithms can be encapsulated, such as mathematical functions, such as ln, sin, tan, etc. These functions do not necessarily belong to any object, so it feels better to call from the class

  • In short, from the perspective of OOA/OOD, all functions that can have a definite behavior without instantiation should be designed as static

  • The most obvious difference between static methods and non-static methods is that if a method is public static, it can be called directly through the method of class name.method name, while public instance methods need to instantiate the object before they can be called.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986754&siteId=291194637