Singleton design pattern in Java - Part 2

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ม.ค. 2025

ความคิดเห็น • 161

  • @ahmetyasarozer
    @ahmetyasarozer ปีที่แล้ว +3

    Having met many experts in the field, and watched thousands of videos, believe me, Ranjith and his vidoes are my favorite. I feel like the world kind of hides a diamond inside it.
    Ranjith, I'm sending my love and appreciation to you. Wish you a beautiful life, together with your family and kids, who are, I'm sure, brilliant as much as you.

  • @MrJonnis13
    @MrJonnis13 8 ปีที่แล้ว +22

    This man is undoubtedly a Java Guru. Deserves much more views and subscribers !!

  • @marekbugiel6893
    @marekbugiel6893 7 ปีที่แล้ว +27

    Excellent job. Very well explained. Taking into account that this is a free video makes your work even more impressive. Respect.

  • @DurgaShiva7574
    @DurgaShiva7574 3 ปีที่แล้ว

    Sir you are GOD of java tech.... and that too free content of this level, hats off !!

  • @akshaygupta2863
    @akshaygupta2863 3 ปีที่แล้ว +2

    Great explanation i have never seen such a good explanaiton. Great job. Can u please some videos on Thread to improve thread concepts

  • @samjam_93
    @samjam_93 4 ปีที่แล้ว +2

    Mass Thala, Never seen such an explanantion, Just by seeing the few videos you posted gained immense knowledge. Love from madurai :)

  • @jating4you
    @jating4you 4 ปีที่แล้ว +1

    your every word is pure and contains tons of experience/information, thanks for creating such videos. please create more.

  • @pranavdixit8577
    @pranavdixit8577 6 ปีที่แล้ว +1

    Really Great Work, Sir !! I am in this field for around 5 years and I can undoubtedly say that this is BEST tutorial for anyone to learn Singleton in all the depths possible !! Cheers !!

  • @darshanpatel9663
    @darshanpatel9663 4 ปีที่แล้ว

    I have never seen this much deep explanation for Singleton..Great.Thank you so much.Waiting for other design pattern videos.

  • @seekingWisdomAcrossWorlds
    @seekingWisdomAcrossWorlds 7 ปีที่แล้ว +2

    This Person deserves a thumps up !

  • @ansoribrahim6833
    @ansoribrahim6833 5 ปีที่แล้ว +1

    very well and extensive explanation. some tutorial hard to explain basic concepts, but this video can explain complex concepts clearly. respect

  • @itskumaresan
    @itskumaresan 6 ปีที่แล้ว

    Appreciate your valuable thoughtfulness in explaining the issues with Singleton and fixing the same!!! Best one that is available out there!!! Keep up the good work!!!

  • @mritunjayyadav3788
    @mritunjayyadav3788 4 ปีที่แล้ว

    your explanations and knowledge in the topic is off the charts sir !

  • @akashagarwal6390
    @akashagarwal6390 3 ปีที่แล้ว

    Underrated channel, trust me when i have been trying to dig deepest into Java since a couple of yrs. This is yrs of knowledge in this video. Do you still make videos? Pls do...

  • @pahariya09
    @pahariya09 5 ปีที่แล้ว

    Awesome sirji.. you have put lot of efforts to explained.... and very nicely explained..

  • @AdityaSingh-kn2hf
    @AdityaSingh-kn2hf 6 ปีที่แล้ว

    Great explanation and handled all the possible scenarios for Singleton. Thumbs up!!!!

  • @JustmeAgainOk
    @JustmeAgainOk 7 ปีที่แล้ว +1

    I was literally clapping at the send of the video , excellent intro to Singleton Pattern , thanks a lot . Can you if possible , throw more insight into the best practices for multi-threaded coding in Java or general.

  • @utkarshgupta2274
    @utkarshgupta2274 2 ปีที่แล้ว

    This was really an in-depth video about the Singleton pattern. I would say this was the best explanation I have seen till now. Great work Ranjith. I request you to please upload more videos on your channel. So that we can learn and grow from your knowledge.

  • @hcabra
    @hcabra 9 ปีที่แล้ว +1

    Thanks Ranjith for this excellent material on Singleton. I believe this is one of the most common yet misunderstood pattern in programming. Your video series simplifies many of the complex notions built around it. Kudos.

    • @ranjithramachandran5928
      @ranjithramachandran5928  9 ปีที่แล้ว

      happy2k thanks for the feedback; yes, most of us takes singleton as the example of design pattern they used, meanwhile it is very hard to get right!

  • @vaibhavshukla9297
    @vaibhavshukla9297 9 ปีที่แล้ว +1

    Really .. really helpful video to understand the singleton and its complex behavior in different scenarios in one go... thanks a lot !!!!

  • @manendranath
    @manendranath 7 ปีที่แล้ว

    One of the best explanations I have ever seen....Thanks Ranjith

  • @siddharthdg
    @siddharthdg 8 ปีที่แล้ว

    Good and precise content , please keep the videos coming .

  • @rajumohan8827
    @rajumohan8827 4 ปีที่แล้ว

    Hello Sir...your session is really motivation for us. Explanation is really well. Nowhere I can find this much of clear and crisp Explanation. Could you please continue making videos for us latest technologies.

  • @gunturmanju
    @gunturmanju 7 ปีที่แล้ว

    Excellent Video Ranjith. Best one so far on web which explains in detail with code from violation standpoint. Thank You!

  • @omtodkar
    @omtodkar 8 ปีที่แล้ว

    FAN of your teaching !! crystal clear with Singleton concept ! great work on video. Keep it up dude !!!

  • @vasuudayar3691
    @vasuudayar3691 4 ปีที่แล้ว

    never seen this much deep explanation.
    i wonder why u stop making videos.
    u helped us alot Thanks

  • @shreyatiwari1331
    @shreyatiwari1331 5 ปีที่แล้ว

    Ranjith, you explained things very well why we have very less number of videos from your end.
    Awaiting more such videos to learn Java from your end..

  • @amitosh14mishra
    @amitosh14mishra 8 ปีที่แล้ว

    Really Nice one , Gone through all your video... w8 for more.

  • @guessmewhoiam
    @guessmewhoiam 9 ปีที่แล้ว

    Thanks Ranjith for crystal clear about Singleton.Most of the doubts got cleared!!!!

  • @aravindraj8893
    @aravindraj8893 5 ปีที่แล้ว

    Detailed Explanation with easy Example. Thank you, Sir

  • @quadcom1
    @quadcom1 7 ปีที่แล้ว

    Ranjith you are awesome. Thanks for your practical explanation.

  • @saktiranjanbehera7010
    @saktiranjanbehera7010 5 ปีที่แล้ว

    Thanks for the facts and solution given after first video

  • @janardhannallapati
    @janardhannallapati 9 ปีที่แล้ว

    @Ranjith i found your videos have the best explanation on Singletons i found so far. Thanks , keep up the good work.

  • @talktosmruti
    @talktosmruti 4 ปีที่แล้ว +2

    @7:29 "But still I would prefer throwing exception thing. Because ....Personal choice"....That's called swag with intelligence....

  • @creative-freedom
    @creative-freedom 9 ปีที่แล้ว

    One of the best java videos on youtube. Hats off !

  • @shaleenmadlapure1405
    @shaleenmadlapure1405 3 ปีที่แล้ว

    very well explained

  • @prakashkchauhannn
    @prakashkchauhannn 8 ปีที่แล้ว

    Nice and Simple explanation. Thank you.

  • @ketankankapure8530
    @ketankankapure8530 3 ปีที่แล้ว +1

    Sir, could you pls continue making videos on rest of the design patterns as well ?

  • @scodz2007
    @scodz2007 8 ปีที่แล้ว

    It was really awesome, really enjoyed both the episodes. Keep up the good work.

  • @meghaladevi6411
    @meghaladevi6411 5 ปีที่แล้ว

    Thank you so much Ranjith. Hats off to you.

  • @suresh85nit
    @suresh85nit 8 ปีที่แล้ว

    Really Really Awesome Explanation..... I have ever heard.. FYi I have subscribed.. Thank You So much for sharing knowledge..

  • @praveenpemal6503
    @praveenpemal6503 8 ปีที่แล้ว

    thanks sir , the singleton videos are best
    waiting for more sir....

  • @rohitsharma-xt8qe
    @rohitsharma-xt8qe 5 ปีที่แล้ว

    Very well explained with crystal clear concepts. Thank you so much for this sir. :)

  • @HariKrishna-bv5hs
    @HariKrishna-bv5hs 8 ปีที่แล้ว

    It's simply Awesome explanation.. Thank you Ranjith.

  • @sanjaybharkatiya
    @sanjaybharkatiya 8 ปีที่แล้ว

    hi ranjith..very very nice and detailed explaination..i have watched all of your vedios...please publish vedios of such kind of expalination for all design patterns you have sumerized in high level distribution of design patterns in one of your vedios...awating of your next vedios on designp

  • @prajaktajawarkar6037
    @prajaktajawarkar6037 5 ปีที่แล้ว

    Thanks Ranjith for such an awesome explanation. This is very helpful for every one who want to learn Singleton pattern. Even all your videos on java are excellent. But those are very limited. I am requesting you to please upload java videos as many you can so that it will help us a lot to improve our knowledge...Please..Thanks again for such a wonderful videos.

  • @peterbittu865
    @peterbittu865 7 ปีที่แล้ว

    Super Ranjith , videos are good and informative.Please make videos on other design patterns as well apart from the factory you have made.

  • @hemalathadinesh1830
    @hemalathadinesh1830 7 ปีที่แล้ว

    Excellent Job, Hats off

  • @SivaKumar-cx7db
    @SivaKumar-cx7db 8 ปีที่แล้ว

    Excellent, no words to say.

  • @anugantigowtham
    @anugantigowtham 8 ปีที่แล้ว

    great tutorial with clear explanation. why new videos are not coming ?

  • @DhrumilShahDhrumil
    @DhrumilShahDhrumil 8 ปีที่แล้ว

    Thanks a lot.. its helps me a lot to understand the concept.

  • @dsingh4800
    @dsingh4800 8 ปีที่แล้ว

    great work ! perfect that you explained that subject so thoroughly with many different aspects, thank you :)

  • @rajeshkundum5622
    @rajeshkundum5622 6 ปีที่แล้ว

    Man, you rocked. Great explanation. keep going.

  • @vipinjainoo7
    @vipinjainoo7 8 ปีที่แล้ว

    Thanks for the videos. Its very helpful.

  • @snehaldhabale1893
    @snehaldhabale1893 5 ปีที่แล้ว

    Hello Sir, your all videos are so well explained and very helpful.
    Could you please post more videos.

  • @Sk-zs1cw
    @Sk-zs1cw 7 ปีที่แล้ว

    Thank you so much for sharing video.

  • @24035709
    @24035709 7 ปีที่แล้ว +1

    How can you be so awesome? Hands down, this is one of the best tech channel I subscribed in recent times.
    Are you on facebook or twitter ? I don't want to miss any updates from you

  • @krish0007
    @krish0007 7 ปีที่แล้ว

    Very good explanation..!

  • @naveenkumar-tb1de
    @naveenkumar-tb1de 5 ปีที่แล้ว

    Excellent ,this really helped.

  • @GiridharKN
    @GiridharKN 9 ปีที่แล้ว

    best tutorial :) awesome thanks alot Ranjith :)

  • @SunilManickalal
    @SunilManickalal 8 ปีที่แล้ว

    Thanks for the videos. very helpful.

    • @naughtyrishan
      @naughtyrishan 8 ปีที่แล้ว

      This solution is fine only. We can also modify the constructor part and that will suffice. Like:
      private Singleton() {
      if(soleInstance != null) {
      throw new RuntimeException("instance already exists. Thus this re-creation is not allowed.");
      } else {
      soleInstance = new Singleton();
      System.out.println("creating singleton instance");
      }

  • @xen_ofc
    @xen_ofc 6 ปีที่แล้ว

    Very cool video.

  • @rajeshnarasimha9926
    @rajeshnarasimha9926 7 ปีที่แล้ว

    Thanks a lot, very well explained..

  • @gurmeetchawla8362
    @gurmeetchawla8362 8 ปีที่แล้ว

    ranjith excellent explaination about singleton. I have seen other videos from you also. All of them are very well covered. Thank you for doing this. I have a request for you. kindly cover units tests also in some video. Please cover various possible situations in that like how to test static classes and static methods. Also how will you go about testing the web application controllers and database related code classes

  • @shubhamchaurasia367
    @shubhamchaurasia367 5 ปีที่แล้ว

    Very well Explained Sir..
    But why u stopped making videos sir..

  • @anshulgoyal7687
    @anshulgoyal7687 9 ปีที่แล้ว

    Hi Ranjit,
    Awesome Explanation on Singleton Design Pattern....Here, it is not clear the flexibility benefit.Can u explore these also.

  • @sreekanth9050
    @sreekanth9050 8 ปีที่แล้ว

    Excellent Tutorial Ranjith. Really Appreciate your in depth Knowledge on this concept. Please Suggest the books that you refer to get this kind of in depth analysis on Core Java Concepts. Thank you and once again super knowledge sharing keep continue..

  • @ckm5101
    @ckm5101 7 ปีที่แล้ว

    Its Very useful , if possible can you please explain multi threading in next vedeo

  • @NaveenKumar-tt6en
    @NaveenKumar-tt6en 6 ปีที่แล้ว

    Thank you so much for the awesome tutorial :)

  • @MrVenkat521
    @MrVenkat521 7 ปีที่แล้ว +1

    Hi Ranjith, Its great explanation, appreciate that. Any thoughts on if new thread try to load it with Class.forName("package.Singleton.class") at some places of invocation in the application will that creates new object/instace for new threads? and what happens to existing instances/reference?

  • @TheDibyendusarkar
    @TheDibyendusarkar 8 ปีที่แล้ว

    Thanks Ranjith for the awesome video. Solution for the multiple class loader problem has not been addressed. Can you put some light on same.

  • @yakoobshaik3980
    @yakoobshaik3980 8 ปีที่แล้ว

    Thank you Ranjith for such a good videos. As you said, we'll have a discussion in comment section regarding the solution for violating singleton rules by creating the instance(s3) and then s1, s2 using Reflection. How can we restrict multiple instances and maintain Singleton pattern? Please answer.

    • @sriramkumar6673
      @sriramkumar6673 5 ปีที่แล้ว

      did you find the solution ?

    • @rohitborate9824
      @rohitborate9824 5 ปีที่แล้ว

      One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.

  • @CHANNEL_for_
    @CHANNEL_for_ 8 ปีที่แล้ว

    Thanks Ranjith...

  • @utkarshjadhav7
    @utkarshjadhav7 7 ปีที่แล้ว +1

    Thank you very much for the tutorial Ranjith. Very informative. Apart from the tutorial, I was curious what font do you use in your IDE. Could you please tell the name of the font?

  • @RiyazParasara
    @RiyazParasara 5 ปีที่แล้ว

    Awesome

  • @harshamr3712
    @harshamr3712 6 ปีที่แล้ว

    Hi Ranjith,. Thanks for the clear information on Singleton Design pattern , Here i have tried the way you mentioned when we move the reflection code before creating the first two instances. and executed then Its creating an instance for reflection as well which has a different hash code and proceeding with creation of next 2 instances with different hash code for both instances . Please advise ,. on how can we overcome this.

  • @VinodKumar-ng3kc
    @VinodKumar-ng3kc 5 ปีที่แล้ว

    Best tutorial!!!

  • @manassinha7115
    @manassinha7115 9 ปีที่แล้ว +1

    Thanks Ranjith... Really a best one ... How can we come to know of your future releases?

    • @ranjithramachandran5928
      @ranjithramachandran5928  9 ปีที่แล้ว +5

      +Manas Sinha Please subscribe to the channel. As a new year resolution, there will be at least 2 vids per month.

    • @manassinha7115
      @manassinha7115 9 ปีที่แล้ว

      Ok thanks...already subscribed...

    • @kaushalpatel91
      @kaushalpatel91 8 ปีที่แล้ว

      +Ranjith ramachandran I have just started watching your videos and I am done with all of them now, and waiting for new one's. I do appreciate your effort on making the video.. if you can spare some time.. that will be great help for developers to learn a thing or two.
      and thanks again for tutorials..

    • @harshavardhan7
      @harshavardhan7 8 ปีที่แล้ว

      :( you broke your resolution :(

  • @piyushasopa9932
    @piyushasopa9932 9 ปีที่แล้ว

    Awesome Explanation on Singleton Design Pattern.......... Thanks Ranjith,
    I will be very happy if you come up with few more design pattern tutorials, Are you thinking of this?

    • @ranjithramachandran5928
      @ranjithramachandran5928  9 ปีที่แล้ว

      +Piyush Asopa Thanks. Please subscribe to the channel for updates. I already have one on factory -method, please check it out th-cam.com/video/EdIwFK0gCm4/w-d-xo.html

  • @IndrajeetKumar-ns8we
    @IndrajeetKumar-ns8we 7 ปีที่แล้ว

    please make more videos, it's very less. your videos have depth analysis of concept which is helpful for working professionals also

  • @sudharshannallamothu3873
    @sudharshannallamothu3873 8 ปีที่แล้ว

    We are awaiting for you next release of other design patterns Mr.Ranjith :)

  • @raghuveerk166
    @raghuveerk166 7 ปีที่แล้ว

    Hello Ranjith, I am missing your videos. I guess all the subscribed persons are also missing your videos. I tried searching your profile in other social websites as well. Can you please come back to youtube? Its also fine if you have paid video series we are willing to enroll, let us know any series if you have. Thanks once again. Awesome work.

  • @shashankagarwal4854
    @shashankagarwal4854 8 ปีที่แล้ว

    Very nice tutorial sir.. :)
    can you tell me which software you are using to share screen..?

  • @muthukumaraRajansk
    @muthukumaraRajansk 8 ปีที่แล้ว

    Nice one

  • @kranthipinninti9086
    @kranthipinninti9086 6 ปีที่แล้ว

    Hi Brother :)
    Your videos are very good.
    Can you make other design pattern videos

  • @javedakhtar-om2hb
    @javedakhtar-om2hb 9 ปีที่แล้ว

    Great Explanation.
    qq#. When we use Reflection to get an Instance, If we keep the reflection code before those Singleton.getInstance(); We are getting the different Instances for Reflection and also for Singleton.getInstance(). How can we restrict to create more than one Instance using Reflection also.

    • @anirbanchatterjee4794
      @anirbanchatterjee4794 6 ปีที่แล้ว

      use like:-
      private Singleton() {

      throw new RuntimeException("Instance already created please use getInstance()");
      }

      private Singleton(boolean actualCreationCall) {

      System.out.println("Only Object Created");
      }
      public static Singleton getInstance() {

      if(onlyObject == null) {

      onlyObject = new Singleton(true);
      }

      return onlyObject;
      }

  • @wBacz
    @wBacz 7 ปีที่แล้ว

    more patterns, please.

  • @LetsBeHuman
    @LetsBeHuman 6 ปีที่แล้ว +2

    3:54 - pastebin.com/sb19AZQd

    • @ManojKumaryOyOmaDy
      @ManojKumaryOyOmaDy 6 ปีที่แล้ว +1

      helpfull comment thankyou dude..... i got output which i have expected

    • @nicoqueijo
      @nicoqueijo 6 ปีที่แล้ว

      I was expecting all three singleton objects to have to same hashcodes but the first one using reflection has a different hashcode than the ones using getInstance. Why?

  • @divyameegada8486
    @divyameegada8486 8 ปีที่แล้ว

    Nice video ranjit.you explain really well. Thank you for such a nice video. I would like to know how can we fix the issue of creating an instance thru reflection when we place the code at the beginning of main method.thanks in advance.

    • @rohitborate9824
      @rohitborate9824 5 ปีที่แล้ว +1

      One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.

  • @92Shreshth
    @92Shreshth 5 ปีที่แล้ว

    Superb video SIr! Thanks a lot.
    What's the answer to the question at 3:47

    • @rohitborate9824
      @rohitborate9824 5 ปีที่แล้ว

      One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.

  • @arnab567
    @arnab567 6 ปีที่แล้ว

    awesome!

  • @anupanandi8186
    @anupanandi8186 7 ปีที่แล้ว +1

    Firstly thanks for tutorial Ranjith.What if the reflection call is there before getInstance method() call?
    There two instances of Singleton class is getting created.How to prevent that?

    • @rohitborate9824
      @rohitborate9824 5 ปีที่แล้ว

      One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.

    • @naveenaryal1400
      @naveenaryal1400 3 ปีที่แล้ว

      @@rohitborate9824 @Rohit Borate Okay but how to get hold of the instance already created? In case the instance is created through reflection is there any way to get hold of that instance in Singleton class?

  • @abhilashk9824
    @abhilashk9824 6 ปีที่แล้ว

    It's the best explanation i have ever seen on the web about the singleton design pattern. I have one query, the way you have followed to avoid singleton breaking by reflection will not work if the first instance is created by reflection and second instance is created by getInstance() method. Could you suggest any way to avoid this as well?

    • @sharathchandra2940
      @sharathchandra2940 6 ปีที่แล้ว

      In the constructor, if you are not throwing an exception, you can assign the value of the private static variable as this, like below
      private static Singleton singleton;
      private Singleton() {
      if (singleton != null) {
      throw new RuntimeException("Cannot create an instance of Singleton, please use getSingleton method");
      }
      singleton = this;
      }

    • @rohitborate9824
      @rohitborate9824 5 ปีที่แล้ว

      One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.

  • @nishitaramanuj3892
    @nishitaramanuj3892 7 ปีที่แล้ว

    Hi Ranjith,
    thums up for your efforts,
    which tool did you use to record this video?
    Thanks,
    Nishita

  • @PradeepMishra-nj5fm
    @PradeepMishra-nj5fm 9 ปีที่แล้ว

    In which conditions we need a Single ton class to be serializable or clonable . If we do't want than can we prevent it by not implementing the interfaces ...

  • @drakezen
    @drakezen 9 ปีที่แล้ว

    Great tutorials. Why aren't you making more?!

  • @Jai_Raj_007
    @Jai_Raj_007 6 ปีที่แล้ว

    You are genius

  • @uniquestreetfood8391
    @uniquestreetfood8391 8 ปีที่แล้ว

    Hi Ranjith, I have a question. If we have a scenario where we are having 2 threads, one thread using reflection to get an instance and other using global point of access getInstance() method.
    Can it still break the code u have provided to take care of reflection.

  • @BipinKumar-ij1mm
    @BipinKumar-ij1mm 5 ปีที่แล้ว

    Hey Ranjith !! It's really excellent . Keep post for new topics.

  • @murugesanrethiam3951
    @murugesanrethiam3951 6 ปีที่แล้ว

    nice....