About the Singleton class

 package com.java2novice.algos;

public class MySingletonExample {

    private static MySingletonExample myObjct;

    static{
        myObjct = new MySingletonExample();
    }

    private MySingletonExample(){

    }

    public static MySingletonExample getInstance(){
        return myObjct;
    }

    public void runMe(){
        System.out.println("Hey, it is working!!!");
    }

    public static void main(String [] args){
        MySingletonExample mse = getInstance();
        ms.runMe();
    }
}

      

Can anyone please provide a description of the above program, why static is used here and why provide a static method to get an object instance?

+3


source to share





All Articles