mercredi 27 novembre 2019

Is it advisable to have a static block inside static inner class

I am in between an implementation where I am planning to use static block inside static Inner class.
This implementation is being done as part of lazy initialization of a singleton class.

I am not able to find any information on google if it is advisable or a good practice to do so?

Any suggestions are welcome.

public class MySingleton {

    private MySingleton() {
    }

    private static class InnerMySingleton {
        private static volatile MySingleton mysingleton;
        static {
            mysingleton = new MySingleton();
        }
    }

    public static MySingleton getInstance() {
        return MySingleton.InnerMySingleton.mysingleton;
    }
}

Aucun commentaire:

Enregistrer un commentaire