Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Singleton Pattern

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

like image 484
Spencer Avatar asked May 14 '10 06:05

Spencer


People also ask

How to create singleton design pattern in Java?

How to create Singleton design pattern? To create the singleton class, we need to have static member of class, private constructor and static factory method. Static member: It gets memory only once because of static, itcontains the instance of the Singleton class.

What is a singleton class in Java?

The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, drivers objects, caching and thread pool. Singleton design pattern is also used in other design patterns like Abstract Factory, Builder, Prototype, Facade etc.

What is a singleobject pattern in Java?

This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class. We're going to create a SingleObject class.

What is meant by eager initialization singleton pattern?

1. Singleton with eager initialization. This is a design pattern where an instance of a class is created much before it is actually required. Mostly it is done on system startup. In an eager initialization singleton pattern, the singleton instance is created irrespective of whether any other class actually asked for its instance or not.


4 Answers

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

like image 67
Bozho Avatar answered Sep 18 '22 16:09

Bozho


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.

like image 34
Etaoin Avatar answered Sep 16 '22 16:09

Etaoin


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.

like image 23
Grzegorz Oledzki Avatar answered Sep 19 '22 16:09

Grzegorz Oledzki


  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.

like image 23
Vaishak Suresh Avatar answered Sep 16 '22 16:09

Vaishak Suresh