Java Singleton Pattern

后端 未结 8 1887
难免孤独
难免孤独 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条回答
  •  Happy的楠姐
    2020-12-03 05:54

    Singleton singleton = Singleton.getInstance();
    

    is the correct way. Make sure your getInstance() method is indeed static.

    Since your Singleton implementation is far from being safe - your object can be instantiated via reflection, you may want to create a singleton based on enum

提交回复
热议问题