18

Edit: Answered - error was method wasn't static

I'm used the Singleton Design Pattern

 public class Singleton {
   private static final Singleton INSTANCE = new Singleton();

   // Private constructor prevents instantiation from other classes
   private Singleton() {}

   public static Singleton getInstance() {
      return INSTANCE;
   }
 }

My question is how do I create an object of class Singleton in another class?

I've tried:

Singleton singleton = new Singleton(); 
// error - constructor is private
Singleton singleton = Singleton.getInstance();
// error - non-static method cannot be referenced from a static context

What is the correct code?

Thanks, Spencer

1
  • FYI, some people avoid the use of get here in getInstance() because of the JavaBean naming convention for properties. An alternative would be instance(). Commented Aug 10, 2014 at 22:47

8 Answers 8

26
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

Sign up to request clarification or add additional context in comments.

3 Comments

That's exactly what I did, forgot to make the method static. Thanks
I don't think one needs to protect code from being abused with reflection. Those who use reflection (should) know the dangers.
@Bart van Heukelom theoretically I agree with you completely. But alas in practice this is often not true :)
3

Singleton singleton = Singleton.getInstance(); should work -- that error doesn't make sense, given your code; are you sure you're reporting it correctly? (It would make sense if you had forgotten to make the getInstance method static, which you've done in your code above.)

The code you've given us for the class is correct.

Lastly, one conceptual note: First, you aren't "creating an object of class Singleton" -- that's the whole point of a Singleton. :) You're just getting a reference to the existing object.

Comments

2

This one:

 Singleton singleton = Singleton.getInstance();

should work. This is how you call static methods in Java. And the getInstance() method is declared as static. Are you sure you are using the very same Singleton class? Or maybe you have imported a class called the same in some other package.

Comments

2
  1. since the constructor is private, it does not make sense to create an object using the constructor.
  2. you should be using public static Singleton getInstance(), but the implementation is not very correct.

    if (instance == null) {
    instance = new Singleton();
    }
    return instance;

This is how you should be doing it. This ensure that it creates the instance if it does not exist, or simply returns the existing instance. Your code would also do the same thing, but this add to the readability.

Comments

2

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.

Comments

1

There is nothing wrong in using

Singleton singleton = Singleton.getInstance();
// error - non-static method cannot be referenced from a static context

This is the way to get the singleton object form the class. There must me something else. Please post some more details

Comments

1

It is still possible to create more than one instance of the class, as follows:

Singleton.getInstance().clone()

Comments

0

since getInstance() method is "static" and instance field too, yo can use Singleton.getInstance(); Without creating new exeple of class. Thihs is the poit of singletone

Comments

Your Answer

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.