8

Why is the Object made static in the Singleton pattern?
What is actual use of it?
What will happen if we don't make object static?

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;
   }

   public void showMessage(){
       System.out.println("Hello World!");
   }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
nilesh
  • 1,483
  • 2
  • 19
  • 37
  • Can you provide a code example? – M A Nov 18 '14 at 11:37
  • See [this](http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) reference for information about the static modifier. – SME_Dev Nov 18 '14 at 11:40
  • 1
    what do you think will happen if it is not static ? you would have a recursive call to the constructor, since for each instance, it contains an instance, which, in turn, contans an instance, and so on, and so forth. – Stultuske Nov 18 '14 at 11:43

3 Answers3

5

You usually keep the single instance of the Singleton class in a static variable of that class. This doesn't make that instance static. Only the reference to it is static.

Since you can only obtain that single instance via a static method of the class (you can't explicitly construct instances of a Singleton class via a constructor - otherwise it wouldn't be a singleton), the reference to that instance must be stored in a static variable.

Eran
  • 387,369
  • 54
  • 702
  • 768
2

Just adding/elaborating eran's answer,since getInstance method is static method, getInstance method can be called from main methods/other methods by using classname i.e. SingleObject.getInstance(); This means you will never need an object reference to call getInstance()

If it would have been instance method , you would have needed an object to call getInstance. Now there is no way to create an object of class SingleObject outside the class (since constructor is private ) & this is the real trouble.

Conclusion 1: This means we need to have a static method.

Conclusion 2: SO NEED TO HAVE A STATIC METHOD, MAKES THE PROPERTY TO BE STATIC, since instance property is not available inside static method.

ASharma7
  • 726
  • 3
  • 8
  • 27
0

What will happen if we don't make object static?

That means that the getInstance method will have to be non-static, because a static method can't refer to a non-static variable in the current class1.

But the getInstance method has to be static ... or this API design doesn't make sense2.


1 - ... unless it is passed a reference to the object, which won't work in this context.

2 - You would need the reference to the singleton instance in order to get the reference to the singleton instance. But you can't create that instance because the class is private ... to stop someone making a 2nd instance.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216