How to Create a Singleton Object


This example shows how to create a singleton object. A singleton is a class of which there can only be one instance in the same Java Virtual Machine.
To create a singleton there has to be a private constructor because the class will itself control the one and only instance that will be created, and of course a private constructor cannot be called from outside the class.
Instead, a method is created with public access that returns the singleton instance (if the method is called the first time the object is instantiated). The example class MySingleton illustrates this:


/**
 * MySingleton.java
 *
 * @author www.javadb.com
 */

public class MySingleton {
    
    
    //the static singleton object
    private static MySingleton theObject;
    
    /**
     * private constructor
     */

    private MySingleton() {
    }
    
    
    /**
     * Checks if the singleton object is created or not,
     * if not it creates the object and then the object is
     * returned.
     *
     * @return the singleton object
     */

    public static MySingleton createMySingleton() {
        
        if (theObject == null)
            theObject = new MySingleton();
        
        return theObject;
    }
}


No matter how many times the method createMySingleton() is called, it will always return a reference to the same singleton object.
This code illustrates this by calling the method twice and then compare the two references. The output of the code is 'true' since they both point to the same singleton object.


/**
 * Main.java
 *
 * @author www.javadb.com
 */

public class Main {
    
    /**
     * Gets references to the singleton
     * instance and checks if they point to the same object.
     */

    public void createSingleton() {
        
        
        MySingleton ms1 = MySingleton.createMySingleton();
        
        MySingleton ms2 = MySingleton.createMySingleton();
        
        System.out.println( ms1 == ms2 );
        
    }
    
    /**
     * Starts the program
     *
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        new Main().createSingleton();
    }
}

Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: