Java Interview Question | What is SerialVersionUID In Java And Why To Use It

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we are going to discuss what serial version uid in Java is and why we need to use it first let us understand what serialization and deserialization in Java is well serialization is a process where a Java object is converted into a stream of bytes and deserialization is the other way around it is the process of converting a stream of bytes into a Java object let us understand with an example here you can see a class person right and we have two data members here age and name and we can see here Getters and Setters for the same we do have two methods zero is person and D series person and serious person you can see we have person object here and we are using file output stream which is an output stream for writing data to a file so we wanted to write this person object into this serialized person.txt file right and we have an object output stream object as well which writes primitive data types and graphs of java objects to an output stream right and we are using write object method which actually writes a specified object in this case person to an object output stream right so this is what the series person method does so to keep it simple so this method just writes this person object into stream of bytes into this file called serious person.txt right and this has to be placed in the current directory of my project and coming to D serial is percent method right so it does the exactly opposite of what we did in serialized person right so we have file input stream and object input streams for reading the file and we we are using read object method to actually read the stream of bytes and convert it into an object and we are caching it into the person object and we are also reading attribute values here so this is what DC release method does so we are going to use this two methods now as you can see in the main method I am trying to actually call the serialized person so you can see here it has to generate the serious person.txt file here in the current directory so let us run this thank you it is an person object series successfully and we can see here series person.txt let us try to open and look into the content so you can see here this file is not readable as this is these are the stream of bytes right so this object is a serialized object now so the serial object has been written into this file all right now how do we read this then so let's go to the D series method now we are trying to deserialize the person object and you can see the object person object has been decided successfully and we are able to get the attribute values back from the serialized object all right so now what I am going to do is I'm just going to add a extra attribute here that's it now going to run it so try to run the you know DC release method trying to read the serialized object and you can see here we get an exception called invalid class exception right so this is because jvm Associates a version number of long type called serial version uid with each serializable class and this ID is used to verify if serialized object and the object loaded during deserialization does have the same attributes and are compatible or not and in this case you can see here so local class incompatible so this is the serial version uid a long number which was the serial version uid generated by jvm for the series object and this is the serial version ID which was generated just now after we added an extra attribute right so as these two does not match so we are getting the class exception and deserialization actually fail so how do we fix it so we can explicitly declare a Serial version ID most IDs can generate this number automatically so you can see here right it does suggest us add serial version uid field so it's a static Final Long data type you can see here right so you can also notice this this is the same number as what we see here right so in for that matter we can have any number defined here right so we have the serial version uid now so I'm going to remove this extra attribute now and going to serial is the object again okay we wanted to run the serialize method right we wanted to repeat the process again with serial version uid added now so objects relate successfully now let us try to deserialize it so we are able to deserialize as well now I am going to repeat the same what we did earlier without adding serial what you need what what did we do we tried adding another extra attribute after which it actually failed DC releasing the object right I am trying the same case now again and this time it actually works we are not getting any exception right so what happens is actually this serial version uid when we see realized was this number and now when we try to DC release as we explicitly Define this serial version uid it actually uses during the residualization process the same serial version uid will be used to match so both will get matched exactly and the person object got DC release successfully so this is the advantage of having serial version uid defined explicitly so in summary it's highly recommended that a class implementing zerosable interface declares its own serial version qid as the generated one by jvm is compiler dependent and thus may result in unexpected exceptions like what we have seen right invalid class exception so in this video what we discussed like we started like understand what serialization and deserialization in Java is and we saw like without serial version uid what's the issue we get and finally adding serial serial version uid we were able to resolve the issue and we understood what's the use of this adding serial a Serial version uid right that's all for this video if you like it please like subscribe and share it thanks for watching
Info
Channel: TechStack9
Views: 3,104
Rating: undefined out of 5
Keywords: java, Java, javaprogramming, coding, javadeveloper, programming, programmer, programminglife, programminglanguage, backenddeveloper, backend, developer, Serialization, Deserialization, SerialVersionUID, Version number, InvalidClassException
Id: VAZOhqFBqDE
Channel Id: undefined
Length: 7min 41sec (461 seconds)
Published: Thu Mar 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.