q0987
q0987

Reputation: 36002

Singleton – the proper way

public enum YourSingleton {
    INSTANCE;

    public void doStuff(String stuff) {
        System.out.println("Doing " + stuff);
    }
}

YourSingleton.INSTANCE.doStuff("some stuff");

Here is the original link, http://electrotek.wordpress.com/2008/08/06/singleton-in-java-the-proper-way/

I am asking why we can call the function doStuff this way in Java.

Upvotes: 5

Views: 348

Answers (4)

lobster1234
lobster1234

Reputation: 7779

You can also use the double-lock singleton creation. Assuming the class is MyObject, has a private constructor, and has a declared a static field instance as null. However, this is not a guarantee that 2 singletons will not end up getting created, but is a much closer attempt to thread safety than a single check.

public static MyObject getInstance()
{
  if (instance == null)
  {
    synchronized(MyObject.class) {  
      if (instance == null)          
        instance = new MyObject(); 
    }
  }
  return instance;
}

Upvotes: 0

John Kane
John Kane

Reputation: 4453

I know this is not really what you asked for but this is what I do when I need a class to be a singleton, which may help. I create one static getInstance method that either creates and returns a new instance of the class if none exist or I return the existing reference of itself, and I make the constructor for this class private.

For example:

public class NameOfClass{
    private static NameOfClass variableReferencingThisClass=new NameOfThisClass();

    private NameOfClass(){}

    public static NameOfClass getInstance(){
        return variableReferencingThisClass;
    }
}

Upvotes: 0

billygoat
billygoat

Reputation: 22004

The Traditional way to implementing singleton is fine, but to maintain its Status as true singleton, it needs to protect itself from sophisticated Serialization and Reflection Attacks. The general way of doing this, is by making the class Implement Serializable, make all instance fields Transient and also implement a readResolve method. (that return the same singleton instance).

The Enum Singleton pattern provides all these features out of the box. But the main reason, I like the Enum variant is its readability. According to me, it conveys what it does, in a much more concise fashion, than a traditional singleton.( You do not have to explain to a new developer, all the vagaries involved in serialization and how serialization might break the singleton guarantee and why you need readResolve method etc etc..)

Upvotes: 2

Travis Webb
Travis Webb

Reputation: 15028

In Java, enum can do everything that class can [1]. YourSingleton.INSTANCE creates an instance of YourSingleton, so you can then invoke methods as if it were a regular class instance, which it basically is.

See the official Java docs for a more in-depth discussion on Enum Types: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

[1] enum does not have a practical implementation of inheritance. Since all enum types implicity inherit java.lang.Enum and Java does not support multiple inheritance, you cannot extend anything else.

Upvotes: 5

Related Questions