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.
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?
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.
@@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 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.
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
Wow ! Very helpful . Need more Java videos like dis . Do more videos.
Hi Ankit, Really awesome videos, watched all your 5 videos. Now, i am having much better understanding. Many Thanks Mate.
Thank you for this video , it really clarifies immutable class requirements .. please make more contents
Ankit you're an awesome teacher.
Great explanation , I have referred many sites ,,,Its the best .
I need more videos like this. Thanks so much.
Thank you. Please like & subscribe. It'll motivate us to put more efforts:)
@@ankit.wasankar yes, i did
Very nice video .. I really understood this concept ✨ thanku sir
you are simply great thanks so much for this ............Lots of love.
You can also create an unmodifiable list in the constructor. Great video. I like your videos.
Awesome content you created, please keep adding more content.. 🙏
Excellent content 👌, why don't you teach whole Java course like this 👍
sure, will try to add more content
@@ankit.wasankar please do.. 🙏...
Seriously, we need it.
Clear explanation 👌😍
Great Job, Your explanation is very great. Kindly make more videos
Excellent ...
So final keyword is not mandatory for instance variable? As we already removed setters
Great content 😊
Excellent content... Please make some more videos on java interview questions.
Thanks and sure
You are just Excellent.
Excellent explanation.
Very good explaination Sir
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?
hey ..awsome videos you have on java......please please upload videos
u said we use constructor for create an object(0.40).....but constructor is used to initialise an object na???
When we us5e "new" keyword, it creates an object and the construction initializes it.
still if this class has any other class (mutable class veriable) veriable declaared that point is missing
List is a mutable instance variable in that class.
thank you for sharing!!!!!
youtube should improve its algo.....it took almost one year to pop this channel. Nice explaination
Very nice explanation
Super video
Nice explanation. Kindly create videos on java 8 features.
How can we add values to an immutable object if needed??
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.
Good explanation
thank you
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?
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.
@@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?
Yeah, we can always create static methods instead of constructor. And private constructor. But that would have further complicated the tutorials.
@@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.
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
Collections.unmodifiableList() can be used as well
Why no new videos ? Kindly do more videos.
Please make more videos .
Bro, just a correction here... constructor can't create object. It will just initialize the object.
then how you create object ..?