Это видео недоступно.
Сожалеем об этом.

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

Поделиться
HTML-код
  • Опубликовано: 14 авг 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

Комментарии • 69

  • @PremSingh-qx7ek
    @PremSingh-qx7ek 3 месяца назад +6

    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 3 месяца назад +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.

  • @umairalvi7382
    @umairalvi7382 3 месяца назад +1

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

  • @TechSavyHere
    @TechSavyHere 2 месяца назад

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

  • @dassneha19
    @dassneha19 6 месяцев назад +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  6 месяцев назад +5

      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.

    • @anishsingla-pg2pd
      @anishsingla-pg2pd 22 дня назад

      I would considering the depth of the videos,, it's heavily discounted

    • @nitinvarshneykiet
      @nitinvarshneykiet День назад

      ​@@ConceptandCoding check negative comments on your videos and u will find a pattern. 😅

  • @digitalanalytics5590
    @digitalanalytics5590 6 месяцев назад

    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?

  • @code4logics
    @code4logics Год назад +3

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

  • @chukwukennedy7035
    @chukwukennedy7035 2 месяца назад

    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.

    • @satyajeetdas6577
      @satyajeetdas6577 29 дней назад

      In Eager Initialization, the dbconnection obj is already created so multiple threads can call the getInstance() method simultaneously , there is no synchronization maintained .

  • @amiyamishra9858
    @amiyamishra9858 4 месяца назад

    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  4 месяца назад

      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 3 месяца назад +1

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

    • @ConceptandCoding
      @ConceptandCoding  3 месяца назад

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

  • @indianhits148
    @indianhits148 2 месяца назад

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

  • @harshalgarg9656
    @harshalgarg9656 Год назад +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  Год назад

      Check this video: see if this help in understanding Volatile keyword and use.
      ruclips.net/video/upfrQvOgC24/видео.html

    • @satyajeetdas6577
      @satyajeetdas6577 29 дней назад

      Writes anywhere when performed are eventually made flushed to Disk for persistent storage , so that in future the same change remain persistent , in general we do read and write from/to caches then Inmemory where writes are periodically flushed to disk . It is something like Write Ahead logs where schedulers are set to periodically make updates to disk . Volatile does the same thing it like instructing thread to read and write from/to memory skipping cache .

  • @PhoenixRisingFromAshes471
    @PhoenixRisingFromAshes471 9 месяцев назад +1

    awesome video

  • @afzalkhan2288
    @afzalkhan2288 7 месяцев назад +1

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

    • @ConceptandCoding
      @ConceptandCoding  6 месяцев назад

      check this : ruclips.net/video/Q6_jrvz-R7w/видео.html

  • @siddharth9703
    @siddharth9703 2 месяца назад

    Hi i started with java palylist but there is no proper sequence followed actually, threads are at last part ,but you used the concept here , did i miss something or you made threads earlier?

  • @karankaira
    @karankaira 4 дня назад

    In singleton enum approach , where are we intializing INSTANCE variable ?

    • @dep2460
      @dep2460 День назад

      They are initlised by default constructor

  • @code4logics
    @code4logics Год назад

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

  • @afzalkhan2288
    @afzalkhan2288 7 месяцев назад

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

  • @shivamyadav4846
    @shivamyadav4846 9 месяцев назад

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

  • @divyanshumaheshwari2868
    @divyanshumaheshwari2868 6 месяцев назад

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

    • @ConceptandCoding
      @ConceptandCoding  6 месяцев назад

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

  • @akarshkumarsrit2518
    @akarshkumarsrit2518 Год назад

    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?

  • @sagarsingh-wb8ou
    @sagarsingh-wb8ou 7 месяцев назад

    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  7 месяцев назад

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

    • @manimanam-d5o
      @manimanam-d5o Месяц назад

      i to got the same doubt read this
      In Java, the `private` access modifier restricts access to the members of a class only within the class itself. However, the nested static class `EagerHelper` is a member of the outer class `Eager`. This means that the outer class `Eager` can access the `private` members of its nested class `EagerHelper`.
      Here's how it works step-by-step:
      1. **Nested Class**: The `EagerHelper` class is a static nested class inside the `Eager` class.
      2. **Private Static Final Field**: The `EagerHelper` class has a private static final field `obj` that is initialized with a new instance of the `Eager` class.
      3. **Access from Outer Class**: The outer class `Eager` can access the `obj` field of its nested class `EagerHelper` because it's a member of the `Eager` class.
      So, even though the `obj` field in `EagerHelper` is `private`, it can still be accessed from the `Eager` class. This is a feature of Java's access control for nested classes. Here’s the explanation with the code:
      ```java
      public class Eager {
      public static Eager Eagerobj;
      private Eager() {
      // private constructor to prevent instantiation
      }
      // Static nested class
      private static class EagerHelper {
      // Private static final field
      private static final Eager obj = new Eager();
      }
      // Public static method to get the singleton instance
      public static Eager getEager() {
      // Accessing the private field of the nested class
      return EagerHelper.obj;
      }
      }
      public class Main {
      public static void main(String[] args) {
      Eager a = Eager.getEager();
      // You can add additional logic here to verify the singleton behavior if needed
      }
      }
      ```
      In summary, the `Eager` class can access the `private` field `obj` of the nested static class `EagerHelper` because it’s within the scope of the `Eager` class, which is allowed by Java's access control rules for nested classes.

  • @roshanjha6466
    @roshanjha6466 Год назад

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

    • @ConceptandCoding
      @ConceptandCoding  Год назад +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.

  • @rabindradocument8934
    @rabindradocument8934 8 месяцев назад

    Can you write subclass for singleton class

  • @umairalvi7382
    @umairalvi7382 3 месяца назад

    How Bill pugs implementation is thread safe ???

    • @ConceptandCoding
      @ConceptandCoding  3 месяца назад

      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 3 месяца назад

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

  • @magesh2353
    @magesh2353 8 месяцев назад

    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 8 месяцев назад +1

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

    • @magesh2353
      @magesh2353 8 месяцев назад

      @@thespptechs2851 thnx man

    • @ConceptandCoding
      @ConceptandCoding  8 месяцев назад +2

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

    • @magesh2353
      @magesh2353 8 месяцев назад

      @@ConceptandCoding hmm okay thanks man

  • @Placement-bv9cg
    @Placement-bv9cg 11 месяцев назад

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

    • @ConceptandCoding
      @ConceptandCoding  11 месяцев назад

      Pls check this ruclips.net/video/upfrQvOgC24/видео.html

  • @harshalgarg9656
    @harshalgarg9656 Год назад

    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  Год назад

      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 Год назад

      @@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  Год назад

      @@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 10 месяцев назад

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

  • @sammykumar1777
    @sammykumar1777 8 месяцев назад

    Sir till when java playlist be completed?

  • @shivaraj17cm
    @shivaraj17cm 8 месяцев назад

    What all do I get from your paid training sources?

    • @ConceptandCoding
      @ConceptandCoding  8 месяцев назад

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

  • @aditigupta6870
    @aditigupta6870 2 месяца назад

    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  2 месяца назад

      kindly check this
      28. BUG in Double-Checked Locking of Singleton Pattern & its Fix
      ruclips.net/video/upfrQvOgC24/видео.html