33

What is the purpose of getInstance() in Java?

During my research I keep reading that getInstance() helps achieve a Singleton design pattern (which means just one instance across the whole program to my understanding). But can't I just use static? Isn't that the whole point of static?

If I were to just have static methods and fields, how would it differ from using getInstance()? Is there a "scope" of static? For example, one instance per method or class?

And if they are different, in what cases would I choose getInstance() over using static?

I apologize if the question is unclear, I am sure I am missing something on the subject matter, I just can't figure out what.

Thank you for any and all advice.

Radek Simko
  • 15,886
  • 17
  • 69
  • 107
FreakyDan
  • 559
  • 1
  • 7
  • 24
  • There is nothing `one instance per method` in JAVA. – Braj Jun 13 '14 at 21:40
  • 1
    `getInstance()` could be a `static` method... – awksp Jun 13 '14 at 21:41
  • 2
    for a singleton pattern, getInstance IS a static method, and uses static attrs. – Guj Mil Jun 13 '14 at 21:42
  • 1
    The whole point of static is to have things associated with the *class* instead of a specific *object*. The singleton pattern guarantees that you will have one instance of an *object* of that type. – azurefrog Jun 13 '14 at 21:42
  • If you are talking about `singleton pattern` then there must be `single instance` of that class it means you can't create its object from `outside the class` so there is only `one way to access` it using `static` method. – Braj Jun 13 '14 at 21:43
  • Search SO a bit and you will find [lots](http://stackoverflow.com/questions/3714971/difference-between-singleton-class-and-static-class) and [lots](http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern) of questions about this. – azurefrog Jun 13 '14 at 21:44
  • You don't need getInstance to create a singleton. – assylias Jun 13 '14 at 21:45
  • Your question has been answered here http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern?lq=1 – apxcode Jun 13 '14 at 21:53
  • 1
    @Braj Thank you for your comment, it wasn't clicking that the purpose of getInstance was to be a accessor. – FreakyDan Jun 13 '14 at 21:53
  • @azurefrog I had read those posts, but since I didn't understand where `getInstance()` was really coming from in the first place, I wasn't able to piece it together. Thank you for the links though, going back and reading them now makes much more sense. – FreakyDan Jun 13 '14 at 22:00

6 Answers6

21

Static will not give you a singleton. Since there is no way of making a top-level class a singleton in Java, you have getInstance methods which will implement some logic to to be sure there is only one instance of a class.

public class Singleton {

   private static Singleton singleton;

   private Singleton(){ }

   public static synchronized Singleton getInstance( ) {
      if (singleton == null)
          singleton=new Singleton();
      return singleton;
   }

}

Check out this top answer: Static Classes In Java

The above code will allow only one instance to be created, and it's clean, however as of Java 1.6, it is better to create singleton's as such since it is slightly more elegant IMHO:

public enum MyEnumSingleton {
    INSTANCE;

    // other useful methods here
} 

Source: http://www.vogella.com/tutorials/DesignPatternSingleton/article.html

Community
  • 1
  • 1
Zac Lozano
  • 778
  • 1
  • 4
  • 12
  • Thank you very much for your explanation. I originally had thought that getInstance Java method (like built in), not a accessor method. – FreakyDan Jun 13 '14 at 21:50
  • Your getInstance() implementation is not suitable for a singleton. – Bohemian Jun 13 '14 at 22:13
  • Ah you're totally right :) I am never setting my instance. I was in a hurry to leave work. Good catch! – Zac Lozano Jun 13 '14 at 22:15
  • That's better! Thank you again @Bohemian – Zac Lozano Jun 13 '14 at 22:19
  • 1
    That's not what I'm talking about. Your method is not threadsafe, so it would be possible to create multiple instances of your class (even with the fix to set the field) – Bohemian Jun 14 '14 at 01:18
  • You show how to make a singleton, but this doesn't answer the question. The question was what is the difference between static and singleton pattern and benefits of one over another. – Gleb Varenov Oct 24 '17 at 08:53
11

Singleton

A singleton allows you to use a single reference to a java Object. For example, here is a singleton which contains a number;

public class MySingleton {

    private int myNumber;
    private static MySingleton instance;

    public static MySingleton getInstance() {
        if (instance == null) {
             instance = new MySingleton();
        }
        return instance;
    }

    private MySingleton() {}

    public void setMyNumber(int myNumber) {
        this.myNumber = myNumber;
    }

    public int getMyNumber() {
       return myNumber;
    }
}

Now we are going to set the value of this number in the A class:

public class A {
    /*...*/
    MySingleton mySingleton = MySingleton.getInstance();
    mySingleton.setMyNumber(42);
    /*...*/
}

Then, you can access this value from another class:

public class B {
    /*...*/
    MySingleton mySingleton = MySingleton.getInstance();
    int number = mySingleton.getMyNumber();
    /*...*/
}

In this class the number variable will have the value 42. This is the advantage of a singleton over a simple object:

All the values stored in the singleton will be accessible from "everywhere".


Static class

The purpose is different, here the advantage is to use an object without having to create it.

For example:

public static class MyStaticClass {
    public static void sayHello() {
        System.out.println("Hello");
    }
}

Now you can use the sayHello() method from any classes by calling:

MyStaticClass.sayHello(); 
G.T.
  • 1,557
  • 1
  • 12
  • 24
  • 1
    you actually can have that behavior with any class (i.e. static methods are always called using the classname regardless of whether the class is marked static or not). The main benefit of an inner static class is that it is not attached to any enclosing instance of Outer class. – Zac Lozano Jun 13 '14 at 22:54
3

The exact method of implementing a singleton, for example using a factory method called getInstance(), isn't that relevant to the question, which is "static methods vs singleton with instance methods".

Classes are themselves effectively singletons, so from that aspect they are similar.

The main difference is that static methods are not part of class hierarchy - they are not inherited, which means the static method option locks you forever to using that exact class and it can't be referred to in any other way, such being an implementation of some interface or a super class.

Instances however don't have this problem, so you can code for example:

class MySingleton implements SomeInterface {
    ...
}

SomeInterface instance = MySingleton.getInstance();
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

I prefer to use static too, but sometimes getInstance() is helpful to have some functions that will be related to the object, in which you can modify variables. if you are simply making some util functions that do not need an instance of an object, use static.

When you are using someone's libraries, you never know if a function body needs a class instance. That's why a lot of library classes are using getInstance().

elixenide
  • 44,308
  • 16
  • 74
  • 100
Victor2748
  • 4,149
  • 13
  • 52
  • 89
1

Instead of checking for null, I like this a little better.

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

Called with...

public class SingletonPatternDemo {
   public static void main(String[] args) {

      //illegal construct
      //Compile Time Error: The constructor SingleObject() is not visible
      //SingleObject object = new SingleObject();

      //Get the only object available
      SingleObject object = SingleObject.getInstance();
   }
}

Full code from: http://www.tutorialspoint.com/design_pattern/singleton_pattern.htm

Jason
  • 1,974
  • 24
  • 19
1

One overlooked reason to use a singleton instead of static class member variables is that the static member variables MAY use space and garbage collector time even in the event the class is never instantiated. Consider how many classes are available to you in rt.jar and what a small fraction of them you probably use in any given application. Also consider that using getInstance ensures your "constructor" has run before any of the member variables are accessed. I almost universally see getInstance as synchronized but I feel this is a bad idea. If anyone ever adds a synchronized blocking method calls to Singleton.getInstance().unsynchronized() could be needlessly held up.

private static Singleton instance = null;
private Singleton() {}
public final static Singleton getInstance() {
    return (instance != null) ? instance : makeInstance();
}
private final static synchronized Singleton makeInstance() {
    return ( instance != null ) ? instance : ( instance = new Singleton() );
}