0

I'm studying static in java but while I am playing with codes, my constructor was only called once (no error) even though I am calling it more than twice using a static method that returns static object. Why is that? please help me understand this.

Output: Constructor called

public class Other {

    private static Other instance = new Other(); // my constructor is here

    private Other() {
        System.out.println("Constructor called ");
    }

    public static Other getInstance() {
        return instance; // this will return the static object declared
    }
}

public class Main {

    public static void main(String[] args) {
        // calling static method 4 times that returns static object
        Other.getInstance();
        Other.getInstance();
        Other.getInstance();
        Other.getInstance();

    }
}
  • 1
    actually, that is not your constructor, it's your constructor call. And it is only called once, so what is the problem? – Stultuske Nov 05 '19 at 07:45
  • 3
    You might want to read a little bit about the singleton pattern. This should answer all the questions about your program – Julius Hörger Nov 05 '19 at 07:50
  • what I mean is the output. I'm calling the method that returns an object constructor but why is it that the output is only `Constructor called` when I call it in my method 4 times? Why not `Constructor called Constructor called Constructor called Constructor called` – user11880836 Nov 05 '19 at 07:50
  • Because you are calling the constructor only once – Julius Hörger Nov 05 '19 at 07:51
  • `private Other() {` is your constructor. `new Other();` is where you are calling your constructor. The constructor is called once, when the class is initialized. Then the value (the constructed object instance) is assigned to the static variable, `instance`. Your static method, `getInstance()` then returns the value that is stored in `instance`. getInstance() does not trigger another constructor call. – Kei Nov 05 '19 at 08:06
  • I wasn't instantiating an object. I was calling my static method that returns an object. Is this code also called singleton? – user11880836 Nov 05 '19 at 08:12
  • 1
    Singleton refers to the pattern in which you have a class that can have only one instance. You are instantiating an object here and only here: `private static Other instance = new Other(); ` You are calling a method (`getInstance()`) that returns the value stored in `instance`. With regards to when the static variable, `instance` is initialized, see: [https://stackoverflow.com/questions/8704423/when-are-static-variables-initialized](https://stackoverflow.com/questions/8704423/when-are-static-variables-initialized) – Kei Nov 05 '19 at 08:16

3 Answers3

2

You have not created the Other object at your main, therefore the constructor has not been called by your main method. Constructors are only invoked when you create an Object.

Instead, you have invoked only static methods returning a class variable (static) that has the Other object set, and as it is a class variable it happened once (that's why you see the constructor has been called once).

The first time the Other.getInstance(); was called, the static instance class variable was initialized by creating the Other object. That happened because this one time initialization procedure takes place only when the class is first loaded.

All the other calls to Other.getInstance();, the class variable was already set and the same Object reference was returned.

Ioannis Barakos
  • 1,319
  • 1
  • 11
  • 16
  • 2
    In order to be initialized a single initialization procedure is run automatically once the class is first loaded. So if you remove Other.getInstance() from the main totally, the Other class will never be loaded and no initialization of instance will occur – Ioannis Barakos Nov 05 '19 at 08:13
1

You're not calling your constructor more than once. You're calling a getter more than once, and that getter gives you an instance of that class which's also a member of the class.

Ssr1369
  • 348
  • 3
  • 16
1

This is called the SINGLETON pattern. The purpose of it is to only have a single instance of a class in a given Java Virtual Machine (application).

The constructor is always private, so it cannot be called from outside the class, i.e. you cannot do Other o = new Other(). In order to get an instance of the class you always have to call the public method getInstance(). The returned instance will be the same each time because the class holds a STATIC reference to the instance. The private constructor is therefore called only a single time during static initialization private static Other instance = new Other();.

2 use cases where you would need only one instance of a class could be:

  • Logging : You create the logger once and then always write to the same logging instance. This prevents the need to sync between multiple logging instances when writing to the same stream (e.g. to a file). Multiple instances writing to the same file could make it corrupted.

  • Configuration : config is global, so it's much more efficient to only have one instance. E.g. config can be read from file at the beginning and the config instance will be reused throughout the whole application.

Hope this helps!

ieggel
  • 891
  • 6
  • 12