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

source

50 Comments

  1. Well done mate, amazingly you have explained this in just 5 mins for which I was seeing more than 20 mins videos. Good job 👍

  2. 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.

  3. Interviewer asked me to write code for singleton class..His requirement is to create 5 objects..if we try to create 6th object it will not create…can we do this…I want answer for this.

  4. 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!

  5. 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.

  6. Is it not the otherway? Meaning, since obj needs to be static (so that its shared among all Abc instances), the getter getInstance() needs to be static.

  7. looks like, another object can be created within class Abc, if that happens it cannot be called as singleton. correct? pls clarify me

  8. 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

  9. I have completed the playlist, and this is the best explanation i have seen on singleton design pattern! Thanks a lot Naveen

  10. 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?

  11. 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?

  12. My way:

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

    usage:
    import static Singelton.SINGLETON;
    SINGLETON.method();

    Is there any problem with that?

  13. 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.

  14. Great tutorial! Thank you for your help. I have a question: if we print the hash code of the obj1 and obj2 we get the same code twice. Why is that ? Should it not be only one hash code?

Leave A Reply

Please enter your comment!
Please enter your name here