{"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