0

I'm trying to make a singleton class, but it returns me a new instance every time. Here is my code.

Did I make any mistake? Why does it return a new instance every time?

It should be the same I think.

public final class SingletonA {     
    private static SingletonA instance;

    private SingletonA(){
    }

    public static SingletonA getInstance() {    
        if(instance==null)
        {
            System.out.println("RETURNING NEW INSTANCE OF SingletonA Class");
            return new SingletonA();
        }
        System.out.println("RETURNING OLD INSTANCE OF SingletonA Class");
        return instance;
    }
}

public class SingletonB {
    public static void main(String[] args) {
        SingletonA one = SingletonA.getInstance();
        System.out.println("1."+one+" | "+one.hashCode());

        SingletonA two = SingletonA.getInstance();
        System.out.println("2."+two+" | "+two.hashCode());

        SingletonA three = SingletonA.getInstance();
        System.out.println("3."+three+" | "+three.hashCode());
    }
}

// HERE IS THE OUTPUT
RETURNING NEW INSTANCE OF SingletonA Class
1.oop.singleton.SingletonA@2a139a55 | 705927765
RETURNING NEW INSTANCE OF SingletonA Class
2.oop.singleton.SingletonA@15db9742 | 366712642
RETURNING NEW INSTANCE OF SingletonA Class
3.oop.singleton.SingletonA@6d06d69c | 1829164700
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
herman shafiq
  • 499
  • 2
  • 8
  • 26

5 Answers5

2

Change like below

if(instance==null) {
    System.out.println("RETURNING NEW INSTANCE OF SingletonA Class");
    instance = new SingletonA();
}
Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21
2

You have to assign the instance otherwise the check will always yield true, thus returning a new instance:

public static SingletonA getInstance() {    
    if(instance==null)
    {
        System.out.println("CREATING NEW INSTANCE OF SingletonA Class");
        instance = new SingletonA();
    }
    System.out.println("RETURNING INSTANCE OF SingletonA Class");
    return instance;
}
hotzst
  • 7,238
  • 9
  • 41
  • 64
2

here is not correct because you are returning new instances of the SingletonA and NEVER assigning it to the object instance..

 public static SingletonA getInstance() {    
        if(instance==null)
        {
            System.out.println("RETURNING NEW INSTANCE OF SingletonA Class");
            return new SingletonA();
        }
        System.out.println("RETURNING OLD INSTANCE OF SingletonA Class");
        return instance;
    }

you mean for sure

 public static SingletonA getInstance() {    
        if(instance==null)
        {
            System.out.println("RETURNING NEW INSTANCE OF SingletonA Class");
            instance = new SingletonA();
        }
        System.out.println("RETURNING OLD INSTANCE OF SingletonA Class");
        return instance;
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2

You should initialize instance to new SingletonA() just before returning in the test for null check.

public final class SingletonA {     
    private static SingletonA instance;

    private SingletonA(){
    }

    public static SingletonA getInstance() {    
        if(instance==null)
        {
            System.out.println("RETURNING NEW INSTANCE OF SingletonA Class");
            // You are missing this assignment. 
            instance = new SingletonA();
            return instance; 
        }
        System.out.println("RETURNING OLD INSTANCE OF SingletonA Class");
        return instance;
    }
}
0
public static SingletonA getInstance() {    
    if(instance==null)
    {
       instance = new SingletonA();
    }
    return instance;
}