17
\$\begingroup\$

Is this the correct implementation of a singleton using enum?

public class Item3 {
public static void main(String[] args) {
 Singleton s=Singleton.Single.INSTANCE.getInstance();
 Singleton s2=Singleton.Single.INSTANCE.getInstance();
 System.out.printf("%b",s==s2);
}
}
class Singleton {
 enum Single{
 INSTANCE;
 Singleton s=new Singleton();
 public Singleton getInstance(){
 if(s==null)
 return new Singleton();
 else return s;
 }
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jun 12, 2013 at 9:56
\$\endgroup\$

3 Answers 3

26
\$\begingroup\$

No, it is simpler to code than that:

enum Singleton
{
 INSTANCE;
 // instance vars, constructor
 private final Connection connection;
 Singleton()
 {
 // Initialize the connection
 connection = DB.getConnection();
 }
 // Static getter
 public static Singleton getInstance()
 {
 return INSTANCE;
 }
 public Connection getConnection()
 {
 return connection;
 }
}

Then you use can use final Singleton s = Singleton.getInstance(). Note however that since this is an enum you can always access this via Singleton.INSTANCE.

And NEVER do that:

public Singleton getInstance(){
 if(s==null)
 return new Singleton();
 else return s;
}

this is not thread safe! What is more, the new Singleton() value is never assigned to s... (thanks @DaveJarvis for noticing this!)

answered Jun 12, 2013 at 10:06
\$\endgroup\$
6
  • \$\begingroup\$ thanks. if I need to obtain a Database connection using singleton in this fashion then I would write enum Singleton{ INSTANCE; Connection c=DB.getConnection(); } and use Connection con = Singleton.INSTANCE.c in client code? Please correct me if my understanding is not correct \$\endgroup\$ Commented Jun 12, 2013 at 10:30
  • \$\begingroup\$ See my edit. While you could make the connection available as you say, it is something I would not recommend personally. \$\endgroup\$ Commented Jun 12, 2013 at 10:37
  • \$\begingroup\$ @fge I know that this will help avoiding multiple objects creation from reflection and deserialization and cloning but will it also avoid creating multiple instances from threading? \$\endgroup\$ Commented Apr 12, 2015 at 15:39
  • \$\begingroup\$ What if you're initialization throws an exception? \$\endgroup\$ Commented Oct 14, 2015 at 20:55
  • \$\begingroup\$ @Mario then try { ... } catch (Whatever e) { throw new ExceptionInInitializererror(e) } \$\endgroup\$ Commented Oct 15, 2015 at 6:30
7
\$\begingroup\$
public enum Singleton{
 INSTANCE;
}

This is enough; you can directly use Singleton.INSTANCE to get the instance of the class.

answered Jun 18, 2013 at 5:39
\$\endgroup\$
0
\$\begingroup\$

Your response and other samples in Singleton_pattern

public enum Singleton {
 INSTANCE;
 public void execute (String arg) {
 //... perform operation here ...
 }
}
answered Jun 12, 2013 at 14:26
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.