ContactUs :

Please send your Questions & Answers or Feedback to "mohan@javabook.org"

Singleton pattern with static factory method

This is one of my favorite method to impelemnt Singleton pattern in Java, Since Singleton instance is static and final variable it initialized when class is first loaded into memeory so creation of instance is inherently thread-safe.

 

 

/**

* Singleton pattern example with static factory method

*/

 

public class Singleton{

    //initailzed during class loading

    private static final Singleton INSTANCE = new Singleton();

 

    //to prevent creating another instance of Singleton

    private Singleton(){}

 

    public static Singleton getSingleton(){

        return INSTANCE;

    }

}

Related Posts Plugin for WordPress, Blogger...
Flag Counter