Reproduced from http://sooxin.iteye.com/blog/796987
Viewpoint 1: (Singleton)
Singleton mode has many advantages over static methods:
First, a single instance can inherit classes and implement interfaces, but static classes cannot (can be integrated but cannot integrate instance members );
Second, a single instance can be delayed for initialization. Static classes are usually initialized during the first loading;
Once again, the singleton class can be integrated, and its method can be overwritten;
Finally, perhaps most importantly, the singleton class can be used for Polymorphism without forcing users to assume only unique instances. For example, you may write only one configuration at the beginning, but you may need to support more than one configuration set in the future, you may need to allow users to load a configuration object from an external file or write your own configuration object. Your code does not need to focus on the global status, so your code is more flexible.
Viewpoint 2: (static method) objects generated in static methods are released after the static method is executed. In addition, when the static method in the execution class is executed, the class where the static method is not instantiated. If Singleton is used, the unique instance generated will remain in the memory and will not be purged by GC (because static attribute variables will not be purged by GC ), unless the entire JVM exits. I thought about this problem for a few days and wrote my own code for an experiment.
Opinion 3: (good !)
Because Dao initialization accounts for system resources, and static methods are used for initialization and release, I personally think that if there is no complicated transaction management, it would be better to use Singleton. Your personal opinion is appreciated.
Summary: Everyone has a consensus on this issue: the instantiation method is more used and secure, and the static method is less used.
Sometimes we have some misunderstandings about static methods and instantiation methods.
1. Everyone thinks"The static method is resident in the memory, and the instance method is not. Therefore, the static method is highly efficient but occupies the memory."
In fact, they are all the same. In terms of loading time and memory usage, the static method is the same as the instance method, and the type is loaded when it is used for the first time. There is basically no difference in the call speed.
2. Everyone thinks"The static method allocates memory on the stack, and the instance method is on the stack."
In fact, it is impossible for all methods to allocate memory on the heap or stack. As code, the method is loaded to a special code memory area, which cannot be written.
The method does not occupy more memory. It has nothing to do with whether it is static.
Because fields are used to store information of each instance object, fields occupy the memory, and because the status of each instance object is inconsistent (at least they cannot be considered consistent ), therefore, the fields of each instance object will be copied in the memory, and you can use them to identify which object you are operating on.
But the methods are different. No matter how many instance objects there are, the method code is the same, so there is only one piece of code. Therefore, no matter whether it is static or non-static, only one piece of code exists, that is, only one portion of memory space is occupied.
Why is the same code running different? This depends on the data used by the method. There are two main data sources: one is to pass in through the parameters of the method, and the other is to use the value of the member variable of the class ......
3. Everyone thinks"The instance method can be called only after an instance is created. It is troublesome and the static method is not needed. It is relatively simple."
In fact, if a method has nothing to do with the instance object of its class, it should be static instead of being written as an instance method. Therefore, all instance methods are related to instances. Since they are related to instances, creating an instance is an inevitable step.
Of course, you can write all the instance methods as static, and pass the instance as a parameter. Generally, no problem may occur.
From the object-oriented perspective, when you choose to use the instantiation method or static method, you should determine whether the method has a logical correlation with the instantiated object, if so, you should use the instantiated object instead of the static method. This is only from the object-oriented perspective.
The instantiation method is recommended for thread security, performance, and compatibility.
Why do we need to distinguish static methods from instantiation methods?
If we continue to study in depth, we will leave the technology to discuss the theory. In early structured programming, almost all methods were "static methods". Introducing the concept of Instantiation is something that will happen after the emergence of object-oriented concepts, the distinction between static methods and instantiation methods cannot be simply understood in terms of performance, creating C ++, Java, C # in this way, the introduction of Instantiation methods by object-oriented language Masters does not aim to solve performance and memory problems, but to make development more modeled and object-oriented. In this case, the distinction between static methods and instantiation methods is to solve the pattern problem.
Let us use another example:
For example, in the "person" class, each person has a name, age, gender, height, etc. These attributes should be non-static, because each person has different attributes; but in terms of biology, the subject, category, and so on, this attribute belongs to the whole human, so it should be static-it does not depend on a specific person, no one of them is a "Spine-animal gate Mammal", but a person is a "hoof.