//*****************************************************************************
// Creating a Singleton Class                          
//*****************************************************************************
class Controller 
{
    private static Controller obj = null;               //a single static object

    private Controller( )                               //private constructor
    { } 

    public static Controller getInstance( )             //getInstance( ) method
    {
        if (obj == null)                                //true for first time only
            obj = new Controller( );                    //call the constructor

        return obj;                                     //either new instance or prev instance
    }

    public String location()
    {
        String str = "Object obj1 at reference: " + this;       //this.toString()
        return(str);
    }       

    public int factoryRand()
    {
        int rand = (int) (Math.random() * 1000000);
        return(rand); 
    }       
}

public class ControllerUse
{
     public static void main(String[ ] args)
     {
        Controller obj1 = Controller.getInstance( );        //create obj1
        System.out.println(obj1.location() );               //new object

        Controller obj2 = Controller.getInstance( );        //create obj2
        System.out.println(obj1.location() );               //returns previous obj

        Controller obj3 = Controller.getInstance( );        //create obj3
        System.out.println(obj1.location() );               //returns previous obj

        System.out.println(obj1.factoryRand() );
        System.out.println(obj2.factoryRand() ); 
        System.out.println(obj3.factoryRand() ); 
    }
}