2

I have a singleton class

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

Consider a public function method() is defined inside the class Singleton.

Which is the best way to call a method inside a singleton class:

Singleton.method() - method calling statically

or

Singleton.getInstance.method() - method not static ?

Doopy
  • 636
  • 8
  • 21
Arundas K V
  • 801
  • 2
  • 14
  • 28

7 Answers7

12

In case of singleton classes there is no use of static methods as there is only one instance available of the class and every buddy is having the same copy of it.

so always create an instance method and call:

Singleton.getInstance().method();
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
2

If you want to use Singleton pattern:

public class Singleton {
    private static Singleton sInstance;

    public static Singleton getInstance() {
        if (sInstance == null) {
            sInstance = new Singleton();
        }

        return sInstance;
    }

    // Prevent duplicate objects
    private Singleton() {

    }

    public void method() {

    }
}
Niko
  • 8,093
  • 5
  • 49
  • 85
  • may i know, what is the actual role of the private method private Singleton() { } ; Instance = new Singleton(); will do the same right ?? – Arundas K V Feb 25 '14 at 10:13
  • 1
    That is the default constructor, so when you declare it private you disallow user to use new Singleton() – Niko Feb 25 '14 at 10:14
  • 1
    only singleton class will have permission to create object and so only getInstance method is a way to get the object instance – vipul mittal Feb 25 '14 at 10:15
  • private does the trick for not have multiple instances of the singleton, without it beign private the singleton is not a singleton – Gastón Saillén Dec 05 '18 at 16:30
2

The singleton pattern allows you to control the amount of instances that exist of one class - i.e. just 1. But the class itself is still a normal class that is not supposed to know how many of it's kind exist and it should therefore have normal instance methods.

When using static methods, you'll run into terrible problems if you ever want to change how many instances of that class exist.

Either use a singleton or use static methods.

zapl
  • 63,179
  • 10
  • 123
  • 154
2

First, you should make sure that you have declared your Constructor as a private one to prevent any one to call it and re-initialize it again. As following:

private void Singleton(){
    //Initialize your private data
}

Second, call the static methods directly as following:

Singleton.yourMethod();

Third:, the non-static method calling as following:

Singleton.getInstance().yourMethod();

Here is a good Example of the Singleton classes

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
1

In the first case:

Singleton.method();

method have to be static

In the second case:

Singleton.getInstance().method();

the method is not static.

So those are conceptually different

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • is it the right way to declare a method statically and call..?? which is the best way to call..in terms of performance?? – Arundas K V Feb 25 '14 at 10:10
  • a Static method is not direct linked to the object instance. You should ask yourself if the method is object-wise or not – Blackbelt Feb 25 '14 at 10:13
  • I believe you need to create Singleton class's object to access its public method when the method is not having a static access modifier. ( first case ) – Lucifer Feb 25 '14 at 10:14
  • @ArunWarrier The difference in performance should be negligible. I don't think you need that sort of optimization – nicopico Feb 25 '14 at 10:16
0

Singleton.getInstance().method();

is better as when called for first time instance will be null and instance is only created in getInstance().

Adhikari Bishwash
  • 2,770
  • 28
  • 32
0

Here you are having getInstance() as a static method and you have created method as a non-static or instance method.

So for a static method, Class.method_name format can be used, but for an instance method object needs to be created.

It may not be the correct syntax as shown over there:

Singleton.getInstance().method();

Singleton obj = new Singleton();
obj.method();

should be the correct format

alexyorke
  • 4,224
  • 3
  • 34
  • 55
abishek kachroo
  • 35
  • 1
  • 11