{"id":12,"date":"2020-07-25T08:02:02","date_gmt":"2020-07-25T08:02:02","guid":{"rendered":"https:\/\/system.camp\/index.php\/2020\/07\/25\/singletonsinjava\/"},"modified":"2020-10-06T09:56:51","modified_gmt":"2020-10-06T09:56:51","slug":"singletonsinjava","status":"publish","type":"post","link":"https:\/\/system.camp\/tutorial\/singletonsinjava\/","title":{"rendered":"Singletons in Java"},"content":{"rendered":"\n
So what is a singleton? In a simpleton\u2019s language, a singleton is a class that will be instantiated only once. This essentially means that there will be only one instance of the Java class in the Java Virtual Machine (JVM). The singleton was initially proposed by the famous Gang of Four<\/a> in their design patterns. It comes under the category of creational design patterns.<\/p>\n\n\n\n The singleton design pattern is used in Logging objects, Java Runtime<\/a>, etc. Good candidates are often the objects which are heavy to initialise. Can you think of any other examples where the singleton design pattern would be useful?<\/p>\n\n\n\n The recipe for making a singleton is quite simple! You would just need:<\/p>\n\n\n\n Enough talk! Let us start with some code which will help us understand the various implementations and their pros and cons.<\/p>\n\n\n\n Let us be lazy first and see what the Lazy implementation is,<\/p>\n\n\n\n Whenever a person decides to call the method Well, being lazy never helped out anyone. Let us try being eager now!<\/p>\n\n\n\n This implementation makes sure the object is initialized only once when the class is loaded. Notice that we have initialized the variable at the very beginning of the class and not inside a function. However, being eager comes at the cost where this object is created even if the client application may not need it. In other words, if the object is heavy to create and it is not used after being initialized, then it is a waste of CPU resources.<\/p>\n\n\n\nLazy initialisation<\/h3>\n\n\n\n
public class Singleton {\n \/\/ static member of the class\n private static Singleton singletonObject;\n \/\/ The private constructor\n private Singleton() {}\n \/\/ Public static member for access outside the class\n public static Singleton getSingletonInstance() {\n if (singletonObject == null) {\n singletonObject = new Singleton();\n }\n return singletonObject;\n }\n}\n<\/code><\/pre>\n\n\n\n
getSingletonInstance()<\/code> for the first time, the object
singletonObject<\/code> is
null<\/code> and the
if<\/code> condition makes sure that the object is initialised and then returned. However, when the subsequent calls are made the object does not have to be initialised anymore and is returned as is. This implementation is called the Lazy implementation because the instance is not initialised until we first need it. But, as you all know, being lazy is not a good thing every time! This implementation comes at the cost of the
null<\/code> check every time. You should also note that this approach is not thread safe.<\/p>\n\n\n\n
Eager initialisation<\/h3>\n\n\n\n
public class Singleton {\n \/\/ static member of the class\n private static final Singleton singletonObject = new Singleton();\n \/\/ The private constructor\n private Singleton() {}\n \/\/ Public static member for access outside the class\n public static Singleton getSingletonInstance() {\n return singletonObject;\n }\n}\n<\/code><\/pre>\n\n\n\n