Java Singleton Pattern

后端 未结 8 1875
难免孤独
难免孤独 2020-12-03 05:22

Edit: Answered - error was method wasn\'t static

I\'m used the Singleton Design Pattern

 public class Singleton {
   private static final Singleton I         


        
8条回答
  •  一生所求
    2020-12-03 06:00

    Since we doesn't want to allow more than one copy to be accessed. So We need to manually instantiate an object, but we need to keep a reference to the singleton so that subsequent calls to the accessor method can return the singleton (rather than creating a new one). Thats why is

    Singleton singleton = Singleton.getInstance();
    

    Correct way to access any singletonObject.

提交回复
热议问题