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
singleton


source to share


No one has answered this question yet

Check out similar questions:

1913
What's so bad about singles?
1638
Difference between static class and singleton pattern?
826
class << self idiom in Ruby
814
Creating a Singlet in Python
791
What is an efficient way to implement singleton pattern in Java?
627
C ++ Singleton design
549
Using singleton dispatch_once model in Swift
2
Can we create a Singleton class by creating a private getInstance method and public getter customization methods?
1
@Singleton with static getInstance method
0
how to ensure that only one instance of a singleton class is created?



All Articles
Loading...
X
Show
Funny
Dev
Pics