13. Java Singleton and Immutable Class Explained with Examples | Java Classes in Depth - Part4

Поділитися
Вставка
  • Опубліковано 8 чер 2024
  • ➡️ Notes Link: Shared in the Member Community Post (If you are Member of this channel, then pls check the Member community post, i have shared the Notes link there)
    ➡️ Join this channel to get access to member only perks:
    / @conceptandcoding
    Topics which i have covered in this video:
    - Singleton Class
    - Eager Initialisation
    - Lazy Initialisation
    - Synchronised Method
    - Double Checked Locking
    - Bill Pugh Singleton Solution
    - Enum Singleton Solution
    - Immutable Class
    - Wrapper Class
    (Wrapper class covered in : • 6. Java Variables - Pa... )
    Chapters:
    00:00 - Introduction
    00:19 - Singleton Class Definition
    02:00 - Eager Initialisation (Singleton)
    05:01 - Lazy Initialisation (Singleton)
    06:28 - Synchronised Method (Singleton)
    08:37 - Double-Checked Locking (Singleton)
    16:14 - Bill Pugh (Singleton) Solution
    18:35 - Enum(Singleton) Solution
    19:52 - Immutable Class
    27:21 - Wrapper Class
    #java #softwareengineer #inteview

КОМЕНТАРІ • 60

  • @PremSingh-qx7ek
    @PremSingh-qx7ek Місяць тому +4

    Hi Shrayansh, I have been following this Java series from sometime and just wanted to shout out that you have made this learning enjoyable for us. You really teach things from first principle which is just amazing. Keep up the amazing work.

  • @umairalvi7382
    @umairalvi7382 Місяць тому +1

    By declaring the inner class as static, it's only loaded into memory when it's referenced for the first time. This means that the instance of the Singleton class is created lazily, i.e., only when the getInstance() method is called for the first time. This helps improve performance by avoiding unnecessary early instantiation.

  • @youknownothingjonsnow3128
    @youknownothingjonsnow3128 7 місяців тому +1

    awesome video

  • @digitalanalytics5590
    @digitalanalytics5590 4 місяці тому

    Great Video Shrayansh. Just a small question for the Immutable class section. Should we not do a deep copy in the parameterized constructor as well, just like the getter method for collection classes? Because the original object can be modified if someone makes an addition to the petNameList right?

  • @snehadas9535
    @snehadas9535 4 місяці тому +4

    If you are making a playlist, make them visible to each users otherwise there is no need ir you can sell your courses. I started following your channel and subscribed it too but here scene is different. One video available and the next is not( available for members with higher price) .. leaving it . Thanks

    • @ConceptandCoding
      @ConceptandCoding  4 місяці тому

      java is always available to members only from start. Few videos made open based on request as many engineers need to watch few videos before taking membership.
      And i dont think 159Rs is higher for complete in depth java playlist.
      But thats my view, but wish you good luck for your future endeavours.

  • @chukwukennedy7035
    @chukwukennedy7035 10 днів тому

    Hello..thank @Shryansh for this educative and awesome content.. I have a question, please anyone who understands the question and has a clue to the answer can still please help me.
    The Question is:
    According to the Singleton session were different types of Singleton class was touched. If the Synchronized Singleton class is solving the two thread issue, which Lazy initialization is facing...Then how Eager initialization managing the two thread issue Synchronize Singleton class is solving.

  • @ashwinn5864
    @ashwinn5864 8 днів тому

    We can create a new arraylist in the constructor and make a copy of the list and that would solve the problem too ., right?

  • @umairalvi7382
    @umairalvi7382 Місяць тому +1

    In the constructor we should do new List(petNamelist);

  • @harshalgarg9656
    @harshalgarg9656 11 місяців тому +1

    Please help in understanding volatile keyword. My though is all operation that jvm performs, heaps, stack are all inmemory(cache). All our operations are having inMemory. that how disk/persistent memory came in picture. Is there any disk memory associate with jvm?

    • @ConceptandCoding
      @ConceptandCoding  11 місяців тому

      Check this video: see if this help in understanding Volatile keyword and use.
      ua-cam.com/video/upfrQvOgC24/v-deo.html

  • @code4logics
    @code4logics Рік тому +3

    can you please cover Multithreading and Concurrency in-depth, Sir?would be big help

  • @afzalkhan2288
    @afzalkhan2288 4 місяці тому

    hi Sj, can you please cover a session that explain Strings in details?

  • @indianhits148
    @indianhits148 6 днів тому

    well 2:38 how can you create object just inside class, it should atleast be inside any method know? like main or something

  • @afzalkhan2288
    @afzalkhan2288 4 місяці тому

    you mentioned we have discussed about string pool, in which video? i couldn't find

    • @ConceptandCoding
      @ConceptandCoding  4 місяці тому

      check this : ua-cam.com/video/Q6_jrvz-R7w/v-deo.html

  • @code4logics
    @code4logics Рік тому

    with CompleteableFuture and async calls and Listenable ..with such topics

  • @akarshkumarsrit2518
    @akarshkumarsrit2518 10 місяців тому

    when we do eager initialisation, there wont be an issue of creating multiple instances of the same class right even if multiple threads are trying to get the object?

  • @rabindradocument8934
    @rabindradocument8934 6 місяців тому

    Can you write subclass for singleton class

  • @sagarsingh-wb8ou
    @sagarsingh-wb8ou 5 місяців тому

    At 17:43 as INSTANCE_OBJECT is private then how can outside class access it as private variable can only be used within the same class.

    • @ConceptandCoding
      @ConceptandCoding  5 місяців тому

      we are using public get and set method to access the private variables

  • @amiyamishra9858
    @amiyamishra9858 2 місяці тому

    Hello Shrayansh, Thank you so much for such great explanation.
    One doubt - in lazy initialization as we have already made the variable to static then how come multiple threads working parallelly create 2 copy of it.. As it is class variable now. I mean to ask why making the constructor private & making the variable to static only does not help??

    • @ConceptandCoding
      @ConceptandCoding  2 місяці тому

      Making the variable static only ensures its shared across instances, but without lazy initialization control, multiple threads can create multiple instances before the variable is initialized, leading to duplicate instances. Making the constructor private prevents direct instantiation, ensuring lazy initialization is properly enforced.

  • @umairalvi7382
    @umairalvi7382 Місяць тому +1

    I went through the community posts but couldnt find the notes of any class. Please tell where to find the notes ???????????

    • @ConceptandCoding
      @ConceptandCoding  Місяць тому

      pls check Member community post. there you will find my post where i shared the notes (some are missing too)

  • @shivamyadav4846
    @shivamyadav4846 7 місяців тому

    in immutable class it should be
    this,petNameList = Collections.unmodifiableList(new ArrayList(petNameList));
    please correct if I am wrong

  • @divyanshumaheshwari2868
    @divyanshumaheshwari2868 4 місяці тому

    Great Video , Shreyansh ! If I want to join this channel wit Membership perks, will this be recurring payment every month??

    • @ConceptandCoding
      @ConceptandCoding  4 місяці тому

      yea but you can compete it within a month or download it too

  • @roshanjha6466
    @roshanjha6466 11 місяців тому

    bhaiya, please increase the frequency of the vedio .. upto when we start springboot??
    ..kindly respond

    • @ConceptandCoding
      @ConceptandCoding  11 місяців тому +2

      10 videos max Java videos left. After that I will start springboot.
      Generally every weekend i upload the Java video, this weekend I got some personal work. But i will upload Java video soon.

  • @magesh2353
    @magesh2353 6 місяців тому

    in an immutable class the member variables need not be final in this implementation i guess, am i correct? coz we cant change the values anyways as there are no setters and also they are not directly accessable

    • @thespptechs2851
      @thespptechs2851 6 місяців тому +1

      Yes, since variables are private and we don't have setters then we don't need to mark them as final.

    • @magesh2353
      @magesh2353 6 місяців тому

      @@thespptechs2851 thnx man

    • @ConceptandCoding
      @ConceptandCoding  6 місяців тому +2

      hi right, its not required, but say its a good practice (just add another layer of protection to the fields)

    • @magesh2353
      @magesh2353 6 місяців тому

      @@ConceptandCoding hmm okay thanks man

  • @harshalgarg9656
    @harshalgarg9656 11 місяців тому

    Bhaiji Lazy initialization me multiple thread bhi aa gaye same time par tab bhi 2 object kaise banenge?? conObject toh static hai na

    • @ConceptandCoding
      @ConceptandCoding  11 місяців тому

      2 threads will try to create object with "new" keyword, memory mein to 2 objects ban jayenge.
      But bcoz of static in code it will keep only 1 reference of the 2 objects created.

    • @harshalgarg9656
      @harshalgarg9656 11 місяців тому

      @@ConceptandCoding So basically it will create 2 objects but both will reference to one only? Other object will be cleared by Garbage collector? If yes then anyway it solves our problem right?

    • @ConceptandCoding
      @ConceptandCoding  11 місяців тому

      @@harshalgarg9656 no that's defeat the purpose of Singleton class.
      2 objects should not be created at any point of time.
      Couple of points:
      - If it's a very expensive to create object, it will be done 2 times.
      - GC will not run instantly. So it will be in memory might be for some time.

    • @Laxmansingh-tl9bq
      @Laxmansingh-tl9bq 8 місяців тому

      @ConceptandCoding Each thread have their own stack memory and if we make object non-static then each individual thread will have its own unique instance variable.
      On the other hand, static members are the part of metaSpace (within heap memory) , so all thread can access only one static instance variable.
      If there is any inaccuracies in my understanding, then please guide me. Thank you sir! your tutorials have been immensely beneficial.

  • @Placement-bv9cg
    @Placement-bv9cg 9 місяців тому

    what is the problem with eager and lazy intilaization can. u explain me clearly??

    • @ConceptandCoding
      @ConceptandCoding  9 місяців тому

      Pls check this ua-cam.com/video/upfrQvOgC24/v-deo.html

  • @sammykumar1777
    @sammykumar1777 6 місяців тому

    Sir till when java playlist be completed?

  • @umairalvi7382
    @umairalvi7382 Місяць тому

    How Bill pugs implementation is thread safe ???

    • @ConceptandCoding
      @ConceptandCoding  Місяць тому

      because it relies on the classloader to initialize the Singleton instance, ensuring that it is created only once in a thread-safe manner.

    • @umairalvi7382
      @umairalvi7382 Місяць тому

      @@ConceptandCoding class loading in Java is inherently thread-safe.
      Can you please explain this statement???

  • @shivaraj17cm
    @shivaraj17cm 6 місяців тому

    What all do I get from your paid training sources?

    • @ConceptandCoding
      @ConceptandCoding  6 місяців тому

      With "Unlock LLD, HLD and Java" level, many interview questions with solutions and many Java videos will be opened up.

  • @aditigupta6870
    @aditigupta6870 16 днів тому

    In the case of double locking can you please explain what will be the case when thread1 preempts after checking null inside synchronised block, its an imp point which i was hoping should have been mentioned in video but its not, it would have been better if things are really explained in a better way and in depth rather than just covering a bunch of topics otherwise it will be of no help in interviews, interviewer asked me this imp point which you missed entirely

    • @ConceptandCoding
      @ConceptandCoding  16 днів тому

      kindly check this
      28. BUG in Double-Checked Locking of Singleton Pattern & its Fix
      ua-cam.com/video/upfrQvOgC24/v-deo.html