05. How to create immutable class - Java Interview Questions

Поделиться
HTML-код
  • Опубликовано: 22 янв 2025

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

  • @AK-tt7fc
    @AK-tt7fc 2 года назад +1

    Wow ! Very helpful . Need more Java videos like dis . Do more videos.

  • @shivttripathhi
    @shivttripathhi 3 года назад +3

    Hi Ankit, Really awesome videos, watched all your 5 videos. Now, i am having much better understanding. Many Thanks Mate.

  • @azizatoumi2523
    @azizatoumi2523 2 года назад +1

    Thank you for this video , it really clarifies immutable class requirements .. please make more contents

  • @deepankarchakraborty236
    @deepankarchakraborty236 2 года назад +2

    Ankit you're an awesome teacher.

  • @bharathr5223
    @bharathr5223 2 года назад +1

    Great explanation , I have referred many sites ,,,Its the best .

  • @nvs4600
    @nvs4600 4 года назад +2

    I need more videos like this. Thanks so much.

    • @ankit.wasankar
      @ankit.wasankar  4 года назад

      Thank you. Please like & subscribe. It'll motivate us to put more efforts:)

    • @nvs4600
      @nvs4600 4 года назад +1

      @@ankit.wasankar yes, i did

  • @shwetapanchal7048
    @shwetapanchal7048 Год назад +1

    Very nice video .. I really understood this concept ✨ thanku sir

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

    you are simply great thanks so much for this ............Lots of love.

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

    You can also create an unmodifiable list in the constructor. Great video. I like your videos.

  • @diwakar.singh_official
    @diwakar.singh_official 3 года назад +2

    Awesome content you created, please keep adding more content.. 🙏

  • @shashankbt6915
    @shashankbt6915 3 года назад +8

    Excellent content 👌, why don't you teach whole Java course like this 👍

  • @nivedhanandh1094
    @nivedhanandh1094 3 года назад +1

    Clear explanation 👌😍

  • @jeganmuthu6962
    @jeganmuthu6962 3 года назад +1

    Great Job, Your explanation is very great. Kindly make more videos

  • @parambharti7095
    @parambharti7095 Год назад +2

    Excellent ...

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

    So final keyword is not mandatory for instance variable? As we already removed setters

  • @ishwaripednekar5164
    @ishwaripednekar5164 5 месяцев назад +1

    Great content 😊

  • @kusumbhatt2168
    @kusumbhatt2168 3 года назад +1

    Excellent content... Please make some more videos on java interview questions.

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

    You are just Excellent.

  • @gaurigupta1368
    @gaurigupta1368 3 года назад

    Excellent explanation.

  • @tannubajpai4782
    @tannubajpai4782 4 года назад +1

    Very good explaination Sir

  • @429nilesh
    @429nilesh 3 года назад

    Hello, I think hobbyList would be persisted in a stack memory not in a heap memory since its reference of a List. Please correct me if I am wrong?

  • @rakeshhariborse2947
    @rakeshhariborse2947 2 года назад +1

    hey ..awsome videos you have on java......please please upload videos

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

    u said we use constructor for create an object(0.40).....but constructor is used to initialise an object na???

    • @ankit.wasankar
      @ankit.wasankar  Год назад

      When we us5e "new" keyword, it creates an object and the construction initializes it.

  • @MHK958
    @MHK958 Год назад +1

    still if this class has any other class (mutable class veriable) veriable declaared that point is missing

    • @ankit.wasankar
      @ankit.wasankar  Год назад +1

      List is a mutable instance variable in that class.

  • @fantasy9960
    @fantasy9960 Год назад +1

    thank you for sharing!!!!!

  • @paggybhangale
    @paggybhangale 3 года назад +1

    youtube should improve its algo.....it took almost one year to pop this channel. Nice explaination

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

    Very nice explanation

  • @sirojiddinsaidov3657
    @sirojiddinsaidov3657 2 года назад +1

    Super video

  • @sagarmandave662
    @sagarmandave662 2 года назад +1

    Nice explanation. Kindly create videos on java 8 features.

  • @priyagoel3743
    @priyagoel3743 4 года назад +1

    How can we add values to an immutable object if needed??

    • @ankit.wasankar
      @ankit.wasankar  4 года назад

      No. We cannot. That's main work of immutable object. That once object is created we can not change state.
      But while creating object we can pass values through constructor.

  • @gnsc
    @gnsc Год назад +1

    Good explanation

  • @aniketbharsakale2561
    @aniketbharsakale2561 3 года назад +1

    Qun1) At 6:38, you mentioned to create a new List inside Student constructor. This violates the basic meaning of a constructor. Constructor is only meant for initialisation of the class field attributes and nothing more. As per you that's true, by creating object inside a constructor u are able to prevent the mutation of that List & achieve immutabiity but you have compromised the meaning of constructor in the first place as per me, isn't it? Ankit.
    Qun2)
    Consider a class Customer which has following fields like (int id; String name; Address address;)
    and class Address(int accountNumber;)
    class Customer "has-a" relationship with class Account
    So now how to make the Customer class immutable?
    Yes, add final to customer class level, the add final to all mutable private fields, no setter methods.
    Considering the above concern of not creating any objects in the target class constructor(Customer) ... How to handle that constructor issue if this constructor issue is considered?

    • @ankit.wasankar
      @ankit.wasankar  3 года назад +1

      Aniket, i think no, we didn't compromise anything here. Constructor is meant to initialize the instance variables and that's what we are doing.
      And this is the only way we can create immutable. So this is a standard coding practice I would say.
      Please let me know your thoughts if anything.

    • @aniketbharsakale2561
      @aniketbharsakale2561 3 года назад +1

      @@ankit.wasankar I mean, it's simple, we create objects and parameterised Ctors are invoked to only "initialise" the object with passed values but not to create any objects inside the Ctors itself. I mean we shouldn't be creating objects inside the ctor itself? Right?

    • @ankit.wasankar
      @ankit.wasankar  3 года назад +1

      Yeah, we can always create static methods instead of constructor. And private constructor. But that would have further complicated the tutorials.

    • @aniketbharsakale2561
      @aniketbharsakale2561 3 года назад +1

      @@ankit.wasankar oh ok. Yea but still I guess you get my point about this small concern. Yes true! about what you said it's a standard approach for immutabiity concept, I understand and I've gone through other blogs and most of them validate this approach. But I faced this as a question(don't create an address class object in ctor, otherwise what's d use of ctor then?) in an interview and honestly I was dumbstruck & was thinking about the same & couldn't answer it instantly & wound up agreeing to what the interviewer claimed. Lol.

    • @ankit.wasankar
      @ankit.wasankar  3 года назад +1

      Could you please help me with any reference which validate that "object creation in constructor is non standard". I'm slightly confused whether it's even true :-)
      I just now checked with sonarlint/sonarqube in intellij. Seems like valid code

  • @punerealestatebuilder
    @punerealestatebuilder 3 года назад

    Collections.unmodifiableList() can be used as well

  • @AK-tt7fc
    @AK-tt7fc 2 года назад

    Why no new videos ? Kindly do more videos.

  • @sandeep-rai07
    @sandeep-rai07 3 года назад

    Please make more videos .

  • @aloneSurvivour94
    @aloneSurvivour94 3 года назад +1

    Bro, just a correction here... constructor can't create object. It will just initialize the object.