Immutable Class - interview insights| java interview questions and answers for experienced/Freshers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey there everyone welcome back and in this video i'm going to talk about an interview question a very popular interview question on immutable objects right so i'm sure that you know how to create immutable classes in java and if you don't that's a very important interview question even i'm going to cover that in this particular tutorial but i'm going to ask you a few interview questions that you might usually get in your java interviews so basically before before i get started with this uh particular tutorial uh the question that i'll be specifically discussing here is how to prevent your immutable object how to prevent your immutable class objects imagine if you have nested objects inside a immutable class how you'll be protecting that how you'll be preventing those objects to be get changed well these questions will make so much sense after some time so let me get started with uh the actual topic and that's basically the immutable objects right so before we get started here is a very simple question what is immutable objects in java or what what do you mean by immutable objects pretty simple and pretty straightforward answer once the object is created it cannot be changed well once the object is created it cannot be changed okay if you see the oracle documentation now here goes the definition once the object is constructed its state cannot be changed well pretty straightforward let me prove that let me prove that with an example then i'll come back to my question which is a very popular one very uh you know important faq for interviews even i got personally asked this particular question so many times in interviews so uh i i will be you know giving you an example first let me prove this particular definition that we have discussed on this immutable object once the object is created it cannot be changed or once the object is created or constructed its state cannot be changed what do you mean by the state of an object well so i got a class here called class app public class app i will be doing one thing here i want to have a main method inside this particular class and i want to create some object here and obviously we need to create objects means we need to create a class so let me go to this interview questions project and i got a folder here called source i just created a you know package here to save some time and i'll go here and i'll be creating a class let's say the class name is student very simple now in this student class let's say i got a you know a variable uh private string student name okay and another one private uh let's say in id okay then two variables i have uh private string student name and private int id maybe i'll generate the getter and setter for this i'll hit control 1 here and i'll do create getter and setter i'll do ok and i got the getter inserter for my id get id and set id and also let me do the same thing for the student name i'll create the getter and setter for the student name the shortcut that i hit here is control one okay so well i i got the id and name uh the getter and setter created for this particular variables i got this object called student now let me go to my app.java class here if i'll be creating a student object student student is equal to new student let me say student avilash my name is avilash hi i forgot to introduce myself hi my name is avilash so basically i'm creating a student object here can i minimize everything now let me do a village dot set id now let's say the id is one now abilish dot set student name and this student name is avilash panikrahi all right so now if i'll be printing the student this out in a village okay now if i'll be printing this i will ask then what will happen let me use i mean run it as a job application now i i am going to get my student object printed here well it's printing the address of the student i just want to do one thing i just want to have a tostring method because i want to print the content of a particular student so right click source create tostring generate two string generate control s go back to your update java here i'm printing the velas instance right now which is this one now let me run this obviously i'll be getting id as one name is village panic right now this is the state of my object right now this is my object is looking like id is one and name is whatever right now let's say um you know if i want i can change the state of that particular object i can change the state of this particular object whatever the object that we have right now here how so basically if i'll do right now avilash dot set id and i am changing the id i'm changing the id just right here now again obvious dot set student name and i'm just making it let's say penny so right now if i'm going to print the village object again swiss out and available all right so this instance i'm printing again here and now if i'll be running this particular program you can see the id and the name is changed and i'm i'm basically printing the same instance here let's see here a village and here also i'm printing the same student instance which is a village right so line number 12 and line number 17 i'm printing the same student instance here this is the object and i'm changing the object state my object was in a state previously now i have changed the state of that particular object that's why this class is not immutable this is mutable class because i'm able to change the state of that particular object which is this guy here i'm creating the object one student object and i'm changing the state of the object uh right here okay i have initialized then i'm changing the state so right now they say immutable that's not an immutable class that's a mutable class then how to convert this class immutable well right now immutable means you cannot change the state of an object remember come back to that definition again once the object is constructed its state cannot be changed well during once the object is constructed state cannot be changed so object is created means it should be there in the same state so to do that i'll come to student.java here i'll be creating a constructor because when the object will be constructed this particular variables will be initialized and to do that i'm going to create a constructor can i do right click source generate constructor using fills where is that there we go now using idn um student name i'll be creating a constructor i'll remove that super there you go so right now here only right i i will be initializing my student during the time of object creation so i'll be going to app.java right now this student is giving me a problem because i don't have a default constructor available inside student.java so let me go there and give the id the id is one and the name is let's say whatever i used to keep previously available right and i will be removing the setters from here because the setters have i have removed because anyhow i'm i'm initializing here only right one and the name is this one so i don't need any setter but still we got the same problem here here i'm printing the villas in stands here so you know i will be getting one in a village panning right then i'm changing the instance again the value of the instance and obviously that instance will be changed run this well it's changed right two and penny previously the object was in different state and now someone is able to change the state of the object by using the setters right now the setters are creating problem right because we are giving the setter so once the object is created here now someone has created the object and we have constructed the object with whatever the value that we have for the idn name right now somebody again is changing the id again changing the name what we do not want so someone is using the setter method because we have the setter method here if i do control click we got the setter method here so what i will do first of all we will remove the setter method and one more thing we will do we'll make this variable final okay final means right nobody will change this particular id and student name agree again once this gets initialized if you remember here we got the constructor right using this constructor someone will be initializing this id and student name come back to the update java when someone will be writing this particular constructor the id will be 1 the value will be available name if i do control click this constructor will be get called and obviously the id and student name whatever we'll be getting will be set to this dot id and this dot student name right once this is initialized once this values are initialized i don't want to give any setter method right now anyhow we are getting error here you can see in line number 18 we are getting a error and in line number 25 we are also getting error why because the student name and id they both are right now marked as final so my compiler is complaining because anyhow we are constructing the object here right now if you are constructing the object here then the id and student name will be initialized and once they are initialized you cannot change the idn student name right so why you are giving the setter remove the setter right you don't have any uh you someone will not have any chance to use the setter method because obviously we are not giving any you know option to the user to change the value because we mark this particular you know variables as final if it is final once the object is constructed right here once the object is constructed this value will be initialized and if this will be initialized obviously you know you cannot use the setter again right so i just remove the setter from here now if we'll go to the app.java you will be seeing right now we here we are having the problem in line number 12 and 13 because we are using a village.set id or we last student name previously so right now we don't have any setter right so obviously right now is complaining that well you don't have set id and state student name so basically if you don't have this method how you'll be using it so let me remove it now obviously i'm not giving any chance to the you know end user to change that id and name once the object is constructed okay this once the student object is constructed the value the one and this particular name this cannot be changed okay so now this particular class is kind of looking like a immutable class if you do control s here now if you run this particular application obviously you can see that previously the object was you know one and uh villa spanish is the name later whenever we are again printing it obviously we got the same value because we have not changed it and we don't have any option to change this of the last object right now because there is no setter method available so now the thing is that okay is this class student is right now immutable [Music] all right so right now by looking into our student class it looks like that we have converted our student class a immutable class so that my class object once constructed this value cannot be changed and of course yes we don't have any setter here for our for our variable id and student name so that nobody will able to set the value uh for this id and student name and obviously even though they will be able to set value our variables are marked as final so you know it cannot be reinitialized again so it looks like we are done there is one more thing you have to do you have to make sure that your class student should be a final class so that no other classes can extend our student class and if they will be able to extend they can override our methods or our variables and they can change the property in different manner well i don't want to give you half information i will come back to this particular thing again why the student class is marked as final when you are creating a mutable class your class should be marked as final but why why is that your interviewer may counter you right we we will talk about that in the next video but right now let me come back to the question that i've asked you during the start of this particular video so three things you have to keep in mind this variable should be final no setter method and your class would be a final class your class should be a final class in order to protect uh this class to be subclassed right so we do not have any give any opportunity to the other developers so that they can extend to our class and can override you know override the getter and the setter method and all the variables or whatever right so this class cannot be subclassed so that's so that i'm i'm using final keyword over here cool so now if i follow these three things right now i feel that my class student is immutable okay so this is what the rule says right but now your interviewer will counter you okay so you feel that your student student class is uh kind of immutable right so we'll say yes it's immutable if you if you create a object of this particular class here line number eight this object you cannot change right you you cannot do like you know once the object is constructed you cannot do a village dot set setters we do not have so how you will be change the object state right argument now your interviewer will counter you okay let's say we got another class which is a mutable class let's say every student has an address okay he got id and student name let's say he will tell you that okay i have another requirement i want to store the address of the student we'll say okay i'll go and i'll create an address class now he'll say okay don't make the address class immutable the address class should be mutable right the address class will not be immutable so you'll say okay no problem i'll just create a class call address so what are the properties you need in the address so let's say i want private uh string you know state i want to store and private string city i want to store so a student city and the student state i want to store so i have taken two variables here city and state i'll go to source and i'll tools and rate getters and setters i'll select everything i'll do generate so i got the getters and setters here maybe i'll create one more constructor to initialize my variables so i'll go to source where is that uh generate constructed using fills and i'll do generate and there you go so i got one constructor called you know address which is gonna take two parameter city and state and which will be initializing the ctn state just right here and also i have given the getter and setter for for my address perfect so this is my mutable class address okay now this class is mutable let me uh do one thing i'll go to student.java right now every student should have an address so what i'll do i'll do private address and address i want to store the address of a particular student and obviously the student class is immutable so i have to make sure that this class should be all the variables of this particular class should be final right uh so line number seven i have made it final as well right now this guy is complaining that well you have to if i'll hover here it is saying that initialize initialize the address address inside the constructor because uh if i'll do equal to null the error should go why because right now the address is initialized and once initialized address will not be changed because it's marked as final so what i need to do i i should not initialize here to null my student should have some address so what i'll do i'll take a address parameter here in the constructor the address address and what i will do i will initialize that this address right here so this dot address which is this guy is equal to whatever you will be getting from the constructor so address here address here close it there you go all right so right now what we can do i will go to my app.java and right here my student class constructor is broken obviously because we have added a new field called address and right now if i'll go to the student class now our constructor has three parameters so let me pass in a address for a student as well so i'll go to app.java here i have to pass the address object okay so now let me create an address object to pass in here so let me first remove this line number 10 not needed right here in line number eight i'll say address address is equal to new address okay let's say address i'll say home address all right uh is equal to i'll be initializing my address here let's say the city is uh that's the bbsr and the state is orisa all right so i got the home address of the student let me pass this variable right here control s right so now in line number nine i am creating i'm constructing my student class right my student class argument my student class constructor will take three parameters one is id then the name and then the address okay so if i'll go to my student class right now obviously this constructor will come into the picture whatever we are giving right here in the parenthesis it will capture that and you will be you know assigning all these things to here so now what i need now here you see here is the interesting thing first of all let me override the tostring here as well because right now the address is not here right i also want to print the address so what i will do i'll just generate the tostring again here so generate to string generate ctrl s now id name and address it is going to print and in the address class also if you go to the address class there also we need to have a tostring if i want to see the content of the address so right click go to source and do a tostring i'll do okay so now go to app.java do control s now let's print this a village instance here this obvious is this guy cool so now let me run this okay now here is the result that we are getting okay id is one name is a village panikrahi and the address is city is bbsr and state is odisha now my object is constructed my student object is constructed and once it's constructed the value will not be changed right now you see here now your student object once is constructed the value of the student object should not be changed because that's the immutable object your student is basically a immutable class here is final here is final there is no getter and setter obviously i need to generate the getter only so let me generate the getter for this uh address i have not generated the getter so maybe i'll just create the getter for the address so i can see get address and get name and get id i only have getters here fine that's that's perfect all right so now this particular class does have a problem okay a very important question a very important question to understand you see here right now it looks like my class is immutable and i have followed all the you know rules to create the immutable class but right now you have to see here that the address instance that you are having here the address reference that you are having here this is my desk final inside the student class which is the immutable class but this address is not a mutable class this address is a mutable class we have getter and setter and everything so now what i can do just look at here so basically i can go to app.java right here i'm constructing my student here i'm printing my student so i'll do abilash dot get address right the immutable class should have getter method right so i'm just using get address right now now i'll do what i'll store it somewhere let's say address address okay now i have a hold to this particular address object okay now i have access to the address of a village this is this guy the address that he got home address which is this one right home address is this one this address i got here right now let me do one thing i'll do address dot set city and i'll change the city to let's say blr or blr that stands for bangalore and the address dot say state to karnataka let's say all right so right now if i will run this application you will see that okay i'm not printing it so let me print this avilas instance again this is my obvious is my student class instance which is a immutable object so let me print that sys out that student object again okay run this there you go now see the address this is your first version of the student id is one name is rahee address is bank uh bbsr in norissa the state is odisha the next one again another student of that the id name is remaining same but see here the address i'm able to change the address of that particular student right right now the student address is changed right now how you can say that your student class is immutable class because right now you are creating your student class object here so once you create a object once your object is constructed that cannot be changed that is the immutable object behavior right but right now here you see once your object got created i did some i have written some line of codes here and that is actually changing your immutable behavior of your of your object so what i did here so if you see i've just created a slide for you where is my slide okay there you go the first slide you just see that so first we got our object called avilash now he got an id and name and the address the address is basically an another object the address is the city is bbsr and state is orisa now this object is constructed and once it's constructed means this cannot be changed now someone is doing a village.get address right now it is basically asking for this address just give me that address so we are going to give him that address so address address so right here he got a reference to this particular object right now what he is doing he is trying to change that particular object right now you see address dot city set city he's setting it to blr address or set state he's setting it to kannada so basically set city set state the setter method we can have the setter method inside a new table object the address is mutable the address is not immutable but the address instance the address reference we have inside the student object which is a immutable object right the student is immutable the address is not immutable right so inside the address we can have getter and setter everything now here inside the immutable object which is the student object now here what we are doing we are doing a village this instance dot get address we are getting the address storing it with the variable and once we get a reference to this particular object now we are changing the city and state and obviously right now the city and state of that particular address will be changed to whatever we have changed right now your immutable property your immutable class property the behavior is broken right so your class student is no longer a immutable class this is your question basically i have taken 20 minutes to explain this one i'm sorry so now you see you have changed the property of your class your property of your address object so now your your student object is no longer a mutable object because of this three lines of code that we have written okay and how you will be protecting it now if i run this you see now you see the address the address is changed for the student object right so the same student object again i'm able to modify his content we should not be done so how we'll be protecting this kind of scenario and that's the question well let's look at the answer for that then [Music] all right so right now here is the question how we are going to protect our class right how we are going to protect our student class which is the immutable class once the object is created it cannot be changed but right now someone is able to change the address address object content uh you know even after the initialization what we do not want because our property is getting broken right and to do that to give you the solution i will again take you to my slide where was my slide okay there you go so right here am i recording okay all right okay so the next thing if i will uh you know run this slide again the fix for this is now here you see now here is my object okay so this is the complete version of my object okay so right now this address is basically you know holding this particular object is in it internally the address is not a primitive type is the class type so it's holding an object internally so right now when someone is going to do a village.get address what we will do we will not give the direct access to this particular object when he will be calling our getter method we will not return him the exact object that our address instance or our address reference is holding okay what we will do we'll just give him a copy of this particular object okay the similar object only whatever address object we have internally the same object will copy it and we'll give it to this guy who is basically asking for the address object right so right now this is a similar object which looks like this right now my address is holding this particular object which is my original object but right now someone when he'll be doing a get address i am not going to return him the original object i will be returning him a clone or i'll be returning him a new object or a similar object which has exactly similar content as this object right now if this guy is taking a hold of this address instance and if he is trying to change the city and the state the problem will be right now the city and state will be changed of this cloned object or this new object that we have returned to him internally whatever the object we got internally this address the student class address this available student class reference right so this is our student object this student address variable is containing this object and we are not giving anybody this particular object we are giving a fake object or a cloned object or a new object which is similar to our object we are giving that kind of address object so if someone is trying to change obviously the object that we have inside our student for address whatever the object we have that object property is not getting changed the cloned object property is getting changed now let's implement it hopefully it makes sense if i'll clone it it's gonna make a lot of sense so code matters right we have to see code right talking will not help okay so now let me go to the app.java okay so right now i'm not going to change anything in this app.java class in this class i won't be changing anything just for clarity i'm running this again and i'm printing my student object two times previously my student object version was this and later i have changed this version to this one the address was getting changed so this is a different version of that particular student so the object is getting changed right now what i will be doing okay now how who is the culprit who is the culprit here this guy is the culprit line number 15 because whenever he is doing a village.get address i am giving him the address object back and right now once you get the hold of that address object he's changing the content well he is doing it by using this get address right click here okay so here in the line number 26 we are returning the address object directly so this address whatever whatever it this the address it hold that object we are giving him directly we don't have to give that object directly we'll create a clone of this particular object or a new address object with the same content and we'll give that object back to the calling method whoever is calling that so right now the get address whenever someone is going to call it i don't want to give a address i don't want to return him the address object directly i'll create a new address object okay and i'm you i'm gonna use the address of that constructor and you remember if i click here uh the address of that constructor i have created few minutes back it is gonna take a city and state okay just right here so now if i'll go to student.java right here the student name the city is gonna be whatever the address is containing the original address is containing gate city that particular city and the original address i'll say address dot get state right the original address state and the original address city with that i'm gonna create a new address that's gonna be the exactly similar address i mean in that address you're gonna have the similar city and the similar state right a sample a copy or a clone of our original address object i with that only i'm creating a new address and i'm going to return that to this whoever is going to call this get address method now you see if you do control s it will go to app.java and right now if you're going to run this particular program see right now look at your address previously look at your address whatever you had before and both these objects are exactly same and the and the person who is trying to trick our application right now will not be able to trick it because right now my student class is completely immutable right it's immutable and even though this address if i do get address and if i click on this address object this is not immutable even though this address is not immutable still we're able to protect our immutable class which is student.java which the student.java is a immutable class and having a new table reference this reference is a mutable reference and it's gonna hold a mutable object so even though someone is trying to change it then we will do what internally this get address method is not going to give the the exact object what the address is holding rather whatever the address is holding with that content we are creating a new object right here in line number 25 with that content whatever the address is having and we are extracting the city and the state and creating a new object and returning it back to the user so if you want if you want to trigger application you'll not be able to do it right [Music] [Applause] hopefully this makes a lot of sense to you right now your interviewer will ask you a lot of counter question and before i go into that i just want to give you a small trick you know maybe if steel is confusing what you can do basically uh the address object that you have here right inside the student class constructor just right here you are creating a student object right and you are you are giving the id the name and the address this is the address okay now what you can do you can print it just right here this out um address object okay um one and you can basically print the address reference or the address has code because if you're going to print the reference obviously we have overridden the two string it is not going to print the object address rather it is going to create the content of the object i want to show the hashcode so i'll just do dot hashcode of that particular object why i'm doing it why i'm doing it i'm gonna let you know so in line number nine see what i did for the home address i'm just getting the hash code and also line number 16 i'm getting the address of object again i'm getting the address back from the overlaps instance the overlash is basically your student class object which is having the address so that address i'm also want to print it says out all right and i'm gonna say address object to and i'm gonna print that address hashcode right all right so now i told you right previously with that diagram that i have shown you okay so if you see the second example here now imagine you got your immutable object which is your student object which got an address and this address is mutable and right now someone when he does a village.getaddress or student.get address at that time we are not returning him the exact object rather we are creating a new object and we are returning him that one so in that case this address that he is going to get this address is basically a different address object isn't it so right now this object is a different object and this object is a different object so now if you are going to go back to your code here i'm trying to print out the hass code in the line number 17 from this address instance so i'm doing address.hashcode and in line number nine right here i'm also doing home address which is this one right dot hash code and you'll see that both the address object are going to be different and you'll be having different hash code you can see this has code and this has code is different right and this confirms that we are getting a different object because this get address method is basically returning you a new object you see a new address object so obviously it's gonna be a different address object right so previously what we used to do remove this address previously we used to do this one return address directly we're returning the address right in that case we are not able to protect our immutable class so if we'll go back to app.java and if you're on the same code you will be seeing that right now you are getting the same hashcode okay same object you some someone is able to get a hold of here in the line number 16 and that's why he is able to change the content of that particular object whatever your immutable object is referring to and obviously that's why you can see um the content of the address is getting changed previously it was vbsr and odyssa letter is changed to blr in karnataka and this address was a part of your immutable object what was student and you are actually having that problem that's why right here in the student class instead of returning the address directly i was doing what i was returning a clone of that i was creating a new address with with the content of the older address that we had or you can just do a direct click clone here that's also going to work well right now this is the explanation like you know how we are going to protect our immutable class [Music] [Applause] but your interviewer might counter you with a different question then he'll ask you that okay i understand you are able to protect your application and you protect your you know immutable object but what about what about the code reliability do you think this this code is pretty reliable do you think this code is good coding do you think this is this is standard approach to do it then obviously the answer should be no this is not a standard approach to you know i mean this code is obviously working but this is not standard why what is the problem that we have you can tell your interviewer that okay this code is not standard that's because what about someone else will give me a requirement tomorrow that in your address.java here in this address class i want to include a new variable i want to include a new variable just like private string uh country right now i want to store the country of a particular student okay then obviously you need to include this country filled here right in the line number seven now if you are doing it obviously in the constructor also right now you have to change it to string uh country okay because obviously you will be initializing that country so country and you will be doing this dot country is equal to country and there you go so right now here is the problem and obviously you will be creating the getter and setter for this country because it's the immutable it's not an immutable class it's a mutable class the address class is a normal class so let me create the country for that the getter and setter for this country so it will go down we got the getter and setter for the country here and maybe right here i will be generate the tostring again because it's only got city and state so let me just to right click source generate tostring and please generate that with all the three fields city state and country okay controllers right now our address class is modified with one more variable now here goes the question right now if you have modified it now in the app.java right here whenever you will be creating an address i will be creating right now we don't have any two argumented constructor we have the three argumented constructor so obviously i have to set a value here called india right which is going to be my country name maybe u.s or uk or something the country of that particular student you have to you have to give it here now the question is whenever you are going to give it here if you see now my problem is fixed now in the student.java we are getting another problem now imagine here in line number 26 whenever someone is going to call your get address method you have to do what you are creating a new address by using the address class constructor and you are basically doing address dot get city address dot guest state and obviously right now we have added one more field to our address class constructor so obviously we need to change this constructor as well we need to say we need to also do address dot get country we have to do that then only the constructor will take three arguments and right now my problem is fixed but now you imagine if if i'll go back to the previous state my code was just like this right two parameter constructor right now imagine i have so many classes just like student class i have teacher class i have uh you know attendant class i have headmaster class i have principal class i have so many classes in every classes if i have used my address constructor just like this it will be broken the code will be broken so i have to go to every class and there i have to fix the constructor by giving it a new argument just like address dot get country okay now this is not a good approach right i mean changing your code again and again is really not good right single responsibility principle solid principle if anybody remember you must have know about this right when we make changes in one class in the address class if you are making any changes that should not impact any other classes you have to take care of that right that is the good coding design pattern if i'm going to create another field here private integer jip right now if i'll be giving one more constructor here what is that int right int jib right and if i'm gonna do this dot jeep uh is equal to jeep right now the code will be again be breaking in every classes and that is not good right i if i if i do any changes to this address class though if my hundred of the other classes that i have if they're also using this class then obviously the code i have to fix there also now imagine 500 classes are using this address class right so everywhere right now every class you have to go just like just like your student class every class you have to go and you have to fix the constructor and that's not the right approach right now let me go to the address.java this jeep is just to show you let me report back my code and then i'll tell you a pics a good coding approach now what you can do you can just go to your student.java right right here you do not create the address object directly here you remove all the parameters okay and right here basically you don't have to filter anything you just send the address object directly control click here this address object directly whatever the address object that you got here so that address object i'll be directly sending it right here i'll be creating a new constructor so new address and i'll be giving the address object itself and inside the address class i will be doing what i'll be creating another constructor so address constructor control space create me address constructor and this address is gonna create a instance of itself okay and i'm gonna do here only from this address i'm gonna do what i'm gonna use a copy constructor so i'll say this basically i am going to do what i'm going to call this particular constructor to call this constructor i need the cte state and the country how i'll be getting it so basically inside this address constructor i'll be doing address which is this one right address dot get city address dot get state and address dot get country all right so basically what i did i just created a new constructor which will take the address instance itself and from here only i'm calling this constructor right in my address class i got this constructor right ct state and country that constructor only i'm calling here using this con this keyword constructor chaining i hope you remember right so right now if i do click on this this you see it's navigating me to this constructor right so what i did here right now it will go to the student.java if i'll do ctrl s you see that basically the error is gone so in the app.java when someone is going to basically call the constructor right here of the student constructor this home address whatever the value of this home address this one this new part whatever you got this object will be stored here right student.java go there this address will be whatever right you can see address address is equal to new whatever the value that you are giving right that object this object basically i'm doing what i am sending it right here in the get method where i have get here basically i'm creating a new object of address if i click here here that address object i'll be getting and right here i'll be creating a new object okay uh how i'll be creating a new object by using this this constructor if you control click here this is basically this constructor i'm calling this constructor only inside the one argumented constructor and i'm filtering with city state and the country right this is the solution for that particular problem and this is a standard coding approach compared to the other one so right now let me remove this comment part and i hope your code will look exactly same as the previous one now if we'll go to the after java and it will be running it obviously uh you know you will be getting different objects here and if you see right here our immutable class property is not changed our class content our object content is exactly similar as the previous one and if we check the content of our object is not changed and our object state is not changed and only thing that we have done here the get address internal implementation we have changed it to this this much this is the new constructor that we have introduced hopefully this is making sense [Music] all right so before we wrap up this particular session uh this is not required but another uh you know approach to solve this particular problem maybe you will see it in a real time code the way they do it basically if you'll go to your app.java whenever someone will call your get address method inside the get address we are basically creating a new instance by using our address constructor and inside the address we just like this some people also create a get instance method a static instance method just like this maybe you will be seeing this kind of you know coding so what what they will do okay some people will do it in this way they will create a get instance method so basically this method will give you the instance of an address because you will be writing this method inside the address class only so the address class will have a get instance method which will return the address object right and here it will accept address object also so address address it will accept address object and return it will return what a new address object and here you can do you can call your address class constructor address dot get ct address dot get uh what is that state what is that get state and address dot get country okay so i'm calling the address class i mean address class constructor if you do a control click here this is the constructor that i'm using that i have inside my address class and basically i am doing what i am having a get instance method and this instance method will return a return a address object and also it will be accepting a address object and also it will be returning address object how it will be returning whatever the address object it will be receiving with that address only it will be facing the city the state and the country and with that it will construct a new object and it and it will give it back to you and i will make sure to make it a static method okay so someone doesn't need to create an object for this particular class to call this get instance method so right now if you'll go to your student.java right here in line number 25 instead of directly creating the object here you can do what you can do address dot get instance and you pass in your address instance just right here and this address is obviously this guy and obviously this address will be initialized right whatever the value that we are giving from the constructor right here copy that paste it always if you don't understand this right this object will be initialized to this one so it will be equal to [Music] paste it here hopefully it's making sense and this address this object will go here to uh to your get instance just right here right and in the get instance basically we are facing everything from the address and creating a new object again of whatever the content we are getting out of that obviously it should make sense uh just practice it one time and just let me know if you can replicate the same thing while you are coding it by yourself but this is another standard approach to do it [Music] all right so before i wrap up this particular session i want to come back to the question that i've asked you in the beginning of the tutorial your interviewer might ask you like um can you tell me the advantages the advantages of immutable object now you told me like you know how to create a mutable object or how to create immutable class what are the pros what are the uh what are the different scenario that you have to look after while you are creating the immutable class that's fine but what are the advantages right okay one advantage one advantage um okay one advantage is basically the thread safety the immutable objects are thread safe now how imagine the normal the mutable objects now if there is a mutable object in a multi-threaded environment then different threads can access that particular object in the same time imagine one thread is reading the data from an object and one thread is writing the data to that object so maybe there is a chance of data inconsistency because if someone is writing something while the other thread is reading maybe the data inside the object will be changed but if the object is immutable then obviously we can guarantee that once the object is constructed obviously the data inside the object will not be changed that's the advantage right uh so you know so even though 10 threads are accessing the same immutable object we can be assured that no uh that thread cannot hamper the data that we got inside that object because that object is immutable so once the object is getting created these threads cannot change the data inside that particular object so trade safety is one reason the next reason the immutable objects can be used as constants right constants just like the value of pi the number of months that we have uh you know of a year the number of days that we have in a year that those are called constant i mean fixed value right the pi value will never change right though um i think you understand so now the another thing is that for for the constant value we can use immutable objects all right one more advantage is the immutable objects can be used as a key in the hashmap if you talk about your hashmap key whenever you create a hashmap you use key value pair right inside the hashmap the key is always immutable you talk about string integer double float whatever you basically take inside inside your hash map the key the hashmap key those are immutable objects right the string is immutable the integer the double everything the wrapper classes are immutable what basically we use as a key in the hashmap so why is that okay why uh okay the benefit is obviously the immutable of this can be used as the key in the hash map but why we use immutable objects as the key in the hashmap that is another question and that is one of the most important question you should not be ignoring this and i will be discussing about this question more in the next video or in the next session and one more thing if you see here we have you the immutable object that we have created which is student object for now let me remove all this value that i have initialized this of this particular class if you'll see you can see that this particular class is preceded by this final keyword and why i made this particular class as final you can you can say to the interviewer that okay this is final because i don't want somebody to extend this particular class then now the interviewer will ask then what then what is the problem if you'll extend it and how you will how you think that your class behavior your immutable class behavior will be changed if someone will be extending this particular class called student which is obviously immutable class if i remove this keyword final from this class called student can i use this class as a key in the hashmap can i use the student class object as a key in the hashmap right question isn't it so how this final keyword is making a difference very important question guys in the next session we'll be discussing that yes if someone is asking about the advantages of the immutable class you can tell them about the trade safety the immutable classes are thread safe the immutable class can be used as a constant okay because the value of the immutable obviously will not change and the constant value also will not change just like the five value the third thing the immutable objects are used as the uh key in the hashmap okay and some more advantages i'll be talking about that in the next session and also i'll be giving you one of the very important questions answered why the immutable classes are used as a key in the hashmap that's coming up in the next session see you soon bye [Music] you
Info
Channel: Selenium Express
Views: 12,530
Rating: 4.9373775 out of 5
Keywords: java interview programs for freshers with answers, core java interview questions, java interview questions, java interview questions and answers for experienced, java interview programs, java interview questions and answers for freshers, java interview questions and answers for 2 years experience, how to make a class immutable in java, custom immutable class in java, java immutable vs mutable, immutable classes in java, top 10 java interview questions, selenium express, immutable
Id: I-HMrM4pskQ
Channel Id: undefined
Length: 57min 32sec (3452 seconds)
Published: Thu Mar 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.