简体   繁体   中英

Constructor was only called once when calling static method twice that returns the static object?

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();

    }
}

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.

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.

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, ie 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 (eg 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. Eg config can be read from file at the beginning and the config instance will be reused throughout the whole application.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM