Singleton patterns and static classes in Java

Source: Internet
Author: User

The singleton pattern and static classes (one class, all methods as static methods) are another very interesting issue that has been exposed in the blog post on Singleton mode interview in Java, because both Singleton and static classes have good accessibility, and there are many similarities between them, for example, Both can be used directly without creating objects, and can be submitted to unique instances, which appear to be used for the same task at a very high height. Because they have more similarities, interviewers often ask a few similar ways to replace a static method with a single-case model for God's horse? Can you replace singleton mode with static classes? What is the difference between a singleton pattern and a static one in Java? Such questions, in order to answer these questions, it is very important to remember that the basic difference between their singleton mode and static method, the former gives you an object, the latter only provides static method, because of the image ratio method has a strong ability, can guide you God when the horse using a singleton mode and static method.

  In this article, we will understand God horse Java in the god Horse Place using a singleton mode, God horse use static class better, by the way, there are several examples of the JDK, and this example is very clever, For example, Java.lang.Math is a final class, and its methods are static methods, and Java.lang.Runtime is a singleton class on the other. For those unfamiliar with singleton or static classes, a static class is a Java class that contains only static methods, and an example of a very good static class is Java.lang.Math, which includes many methods for implementing tools for different data functions, such as sqrt (), and singleton classes, There is only one instance for the entire application life cycle, such as Java.lang.Runtime.   God horse when using static class substitution Singleton in Java there are some scenarios where static analogy is more suitable, One of the main examples in this scenario is Java.lang.Math, which is not singleton, and all of its methods are static methods, here I give a few scenarios, I think the use of static analogy singleton mode is more suitable. 1)         If your singleton does not need to maintain any state, only provides the method of global access, this case considers using static class, static method is faster than Singleton, because static binding is done at compile time. Keep in mind, however, that it is not recommended to maintain state information in static classes, especially in concurrent environments, which can cause bad race conditions if there are no appropriate synchronization measures to modify multithreading concurrency. If you need to put some tool methods together, you can choose to use static methods, but other things that require singleton access to resources should use Singleton mode. The difference between a single case and a static one in  java this is the answer to the second interview question about Singleton and static, as I mentioned earlier, their basic difference between a performance class, a presentation method, and the following list of differences between them. 1)         Static analogy Singleton has better performance because static methods are bound at compile time. 2)         Again, the difference is the ability to override, because static methods in Java are not covered, which leads to too much flexibility in their wood, and on the other hand, you can override the methods defined in the Singleton class in a way that inherits. 3)         Static classes are difficult to simulate, so it is difficult to test single-case, single-case easier to simulate, because it is also easier than static classes to write unit tests, regardless of God horse single-case God horse, you can pass simulation objects, such as construction methods or method parameters. 4)         If youneeds to maintain state information, the Singleton is more appropriate than a static class because the latter is very scary in maintaining state information and leads to a slippery bug. 5)         If it is a very heavy object, the singleton can be lazy to load, but the static class does not have this advantage and is very eager to load. 6)         Many dependency injection frameworks have good management of the singleton, such as spring, which is very easy to use. These differences between the two help us to make choices in the face of some scenarios, and the next section to learn about God's horses when choosing a singleton rather than a static class. In Java, the advantages of selecting a singleton instead of a static class The main advantage of the single example and static is that the former is more object-oriented than the latter, and using singleton can extend the base class through inheritance and polymorphism, realize interface and more ability to provide different implementations. If we discuss java.lang.Runtime, which is a singleton in Java, the call to the GetRuntime () method will return different implementations based on different JVMs, but it also guarantees that there is actually an instance in each JVM. If Java.lang.Runtime is a static class, it is unlikely that different implementations will be returned by different JVMs.   This is the difference between a singleton and a static class in Java, and when you need an object with full OO capability, choose Singleton, if you just pre-sell some static methods, use static classes.

Singleton patterns and static classes in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.