0

In Java you can create a singleton like this:

public class SingleObject {

   //create an object of SingleObject
   private static SingleObject instance = new SingleObject();

   //make the constructor private so that this class cannot be
   //instantiated
   private SingleObject(){}

   //Get the only object available
   public static SingleObject getInstance(){
      return instance;
   }
}

Is there any reason why you just couldn't make instance public and dispense with getInstance?

Johann
  • 27,536
  • 39
  • 165
  • 279
  • 2
    Some one else could change the instance -> not a singleton anymore. – SBI Dec 21 '15 at 16:18
  • If its public static then it becomes class variable accessible to outside world. If its only public you can't use it inside getInstance(). – Shriram Dec 21 '15 at 16:19
  • 3
    if you want to make it `public` then use `final`. – YoungHobbit Dec 21 '15 at 16:19
  • @YoungHobbit Unless you want a lazy singleton. – crush Dec 21 '15 at 16:20
  • @crush Yes. That OP is already doing. So I did not go into that. – YoungHobbit Dec 21 '15 at 16:21
  • @crush If you want a lazy singleton you pretty much have to use a getter of some kind for your instance, with proper null-checking and thread safety etc. – dcsohl Dec 21 '15 at 16:22
  • @dcsohl Right. Which is why a public final field wouldn't work for that case. – crush Dec 21 '15 at 16:22
  • possible duplicate: http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – guillaume girod-vitouchkina Dec 21 '15 at 16:28
  • The duplicate made by @Sotirios Delimanolis is not OK, it is about lazy instanciation. Vote to reopen. – ouah Dec 21 '15 at 18:55
  • @ouah What? The question there states _This is a lot shorter, faster as there's no null-check, and typing `MyClass.instance` feels nicer to me than typing `MyClass.getInstance()`. Is there any reason why the second is not the mainstream way to do this?_ The answers there address the flexibility you spoke about in your answer while also addressing lazy initialization that is available through `getInstance`. They also propose the `enum` solution. – Sotirios Delimanolis Dec 21 '15 at 19:05
  • @SotiriosDelimanolis Look again, the duplicate question compares lazy instanciation with non-lazy instanciation. In OP code here lazy instanciation is not the subject. There are probably duplicates to this question but the one you used for the duplicate is not the right candidate. – ouah Dec 21 '15 at 19:15

2 Answers2

3

The modern (since Java 1.5 I think) canonical way to create singletons in Java, shamlessly copied from this answer, which has a lot more background and larger example, is this, which replaces the code in your question:

public enum SingleObject {
    INSTANCE;
}

To answer your question directly: you are asking a wrong question, and the answer is, it shouldn't be either of the alternatives you give.

Community
  • 1
  • 1
hyde
  • 60,639
  • 21
  • 115
  • 176
  • 4
    Am I the only one who still doesn't like using `enum` in this way? Of course, I don't create many singletons these days. – crush Dec 21 '15 at 16:29
  • @crush Probably not. However, I think in this case the trade-off is well worth the somewhat confusing use of keyword `enum`, any alternative would be worse. – hyde Dec 21 '15 at 16:30
  • Doesn't `enum` just compile into `public static final SingleObject INSTANCE = new SingleObject()` (as a member of `SingleObject` class)? It seems at best it removes some very simple boilerplate, while making it unclear what role the structure plays and introducing some unneeded meta data `$values` – crush Dec 21 '15 at 16:32
1

In Effective Java, Joshua Bloch says on the static factory method advantage over the public final field is:

One advantage of the factory-method approach is that it gives you the flexibility to change your mind about whether the class should be a singleton without changing its API. The factory method returns the sole instance but could easily be modified to return, say, a unique instance for each thread that invokes it.

And he also says:

A second advantage, concerning generic types, is discussed in Item 27. Often neither of these advantages is relevant, and the final-field approach is simpler.

Otherwise in Java he writes that "a single-element enum type is the best way to implement a singleton":

// Enum singleton - the preferred approach
public enum Elvis {
    INSTANCE;
    public void leaveTheBuilding() { ... }
}
ouah
  • 142,963
  • 15
  • 272
  • 331