For code click here: The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one …



  1. Nice explanation. Here I found that, Inside Abc class, "static Abc obj = new Abc();" is defaulted to a public statement, so, inside the main method, java will allow accessing directly static obj object. eg. "Abc obj1 = Abc.obj;", so getInstance() method is never needed to use. To fix this, we can mention like "private static Abc obj = new Abc();" and getInstance() method can be used now.

  2. Firstly, it was hard for me to understand the concept of Singleton pattern even after watching 4-5 videos, but your one video was more clear and was able to understand within few minutes. Thanks for the good stuff!

  3. Hello Sir,
    I have one question
    If Two threads are accessing the Singleton class object at a same time then what will happen or what problem will come and how we resolve such problem.

  4. i think this singleton class is not working in multi threaded environment and we the static variable is always loads at the time of loading the class so the object is stored in jvm memory if we dont use also the jvm memory will be waste

  5. navin, I am writing a web service using rest. How do I use the singleton pattern to create a list that will keep getting updated with new elements with every @POST? The problem is that if I use a regular constructor, then whenever the rest service calls that class using a @POST , a new instance of that class is created with the same elements as before, and so the new element added by the previous @POST request to that class does not persist. Now obviously, I can overcome this limitation by updating the date into a table, but is there a way to do this without using a table?

  6. It is showing an error on class object "The field obj cannot be declared static in a non-static inner type, unless initialized with a constant expression" so make the class also static. Please let me know why it is so?

  7. My way:

    public final class Singleton {
    public static final Singleton SINGLETON = new Singleton();
    private Singleton() {}

    import static Singelton.SINGLETON;

    Is there any problem with that?

  8. It would so good if you gave it with a real life example.Syntax is easy to learn but the implementation in programming daily life problems is so important here.
    But the video and the explanation is amazing.


Please enter your comment!
Please enter your name here