-4

I don't fully understand the concept of Singleton class in Java. For example I have singleton class like this:

private int i = 0;
private static Singleton INSTANCE = new Singleton();

public static Singleton getInstance() {
    return INSTANCE;
}

public int getI() {
    return i;
}

public void setI(int i) {
    this.i = i;
}

So every time I call getInstance() isn't it creating a new Object of Singleton because of this: new Singleton() ?

Then why if I initializes it like this :

Singleton s = Singleton.getInstance();
    Singleton s2 = Singleton.getInstance();

    s.setI(5);

    System.out.println(s.getI());

    System.out.println(s2.getI());

I'm getting '5' two times. Shouldn't it write 0 to me the second time? I'm truly missing something but no idea what.

  • At `Singleton INSTANCE = new Singleton();` part `new Singleton()` creates new instance of `Singleton` type, and *returns reference to it* (reference = number which JVM uses to identify objects) which is stored in `INSTANCE` *variable*. At `return INSTANCE` method is returning *reference* (number) representing that one Singleton instance which was created earlier, no additional Singleton instance needs to be created. – Pshemo Aug 08 '20 at 13:38
  • 4
    It seems like the problem is that you don't understand how a variable works. – khelwood Aug 08 '20 at 13:39
  • Possibly related: [What is the difference between a variable, object, and reference?](https://stackoverflow.com/q/32010172) – Pshemo Aug 08 '20 at 13:41
  • Bad construction of singleton. Your constructor should be private. Read more about pattern designs and singleton here https://howtodoinjava.com/design-patterns/creational/singleton-design-pattern-in-java/ – D A Aug 08 '20 at 13:42
  • So returning `INSTANCE` aren't we creating new Singleton() every time? – Boharuk123 Aug 08 '20 at 13:57
  • 1
    @Boharuk123 No.. it's a `static` variable. There is only one for the entire class. I suggest you read Oracle's Java tutorial on [Understanding Class Members](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – Scratte Aug 08 '20 at 14:03
  • @Scratte thank you for this answer, I read this but personally for me `new Singleton()` is creating new object every time I call this. So how this is working?? First time it just creating this object and second time what is going on? Somehow JVM is checking if this object was already created? – Boharuk123 Aug 08 '20 at 14:50
  • 1
    @Boharuk123 It doesn't need to check. It creates it when the class is first loaded. After that when you call `Singleton.getInstance();` you just get the same value every time. (This is also a static method, so you don't use an object to call the method.) The value is that one object that was created. The `INSTANCE = new Singleton();` is just the initialization of the variable. It never gets initialized more than once. You can check that they are the same by using `==` which will check that they have the same reference, like so `System.out.println("same Singleton: " + (s == s1));` – Scratte Aug 08 '20 at 15:00
  • But.. your code is missing the private constructor to be a Singleton. If you have a public constructor, you can create it from outside just using `new Singleton()`. You can do the same inside the class because private only protects against the outside, but that would defeat the purpose, like if you make it like this `public static Singleton getInstance() { return new Singleton(); }` – Scratte Aug 08 '20 at 15:13
  • @Scratte Yes I know about private constructor, thank you very much for your time. One last thing. This class is first time loaded when I call Singleton.getInstance() for the first time? – Boharuk123 Aug 09 '20 at 11:22
  • If I remember correctly, yes. The class is loaded when you call that static method the first time. Java does not load classes unless they are needed. – Scratte Aug 09 '20 at 21:13

2 Answers2

1
    INSTANCE = new Singleton();

would only get called once as it is static. Thereafter:

    getInstance()

only ever returns that static instance

0

Singleton is a just 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.

and to Implement that u should :

  1. Private constructor to restrict instantiation of the class from other classes.

  2. Private static variable of the same class that is the only instance of the class.

  3. Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class.

    public class SingleObject {

    private int i = 0;
    //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 int getI() {
        return i;
    }
    
    public void setI(int i) {
        this.i = i;
    }
    

    }

DEV
  • 1,607
  • 6
  • 19
  • i think what u did, doesnt have anything to do with singleton Pattern u just created a static property wich is named instance, and this property will be shared between all the instance of the class Singleton. beside s and s2 will have the same reference to the same instance that's why it will always return the same value of i. – DEV Aug 08 '20 at 14:07