{"id":1468,"date":"2017-03-21T17:18:27","date_gmt":"2017-03-21T11:48:27","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1468"},"modified":"2017-03-29T11:01:31","modified_gmt":"2017-03-29T05:31:31","slug":"java-serialversionuid-use","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-serialversionuid-use\/","title":{"rendered":"JAVA &#8211; What is a serialVersionUID and why should I use it"},"content":{"rendered":"<p><span style=\"color: #ff6600;\"><b>SERIALIZATION:<\/b><\/span><\/p>\n<ul>\n<li>To\u00a0<b>serialize<\/b>an object means to convert its state to a byte stream so that the byte stream can be returned back into a copy of the object.<\/li>\n<li>A\u00a0<b>Java<\/b>object is\u00a0<b>serializable <\/b>if its class or any of its super classes implements either the\u00a0<b>java<\/b>.io.<b>Serializable<\/b> interface or its subinterface,\u00a0<b>java<\/b>.io.Externalizable.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><b>DESERIALIZATION:<\/b><\/span><\/p>\n<ul>\n<li>The reverse process of creating object from sequence of bytes is called\u00a0<b>deserialization<\/b>.<\/li>\n<li>A class must implement Serializable interface present in\u00a0<b>java<\/b>.io package in order to serialize its object successfully.<\/li>\n<li>Serializable is a marker interface that adds serializable behavior to the class implementing it.<\/li>\n<\/ul>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\" wp-image-1469 aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/ol-300x139.png\" alt=\"\" width=\"509\" height=\"236\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/ol-300x139.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/ol.png 620w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/><\/p>\n<h4 id=\"uses-of-serialization-and-deserialization-in-java\"><span style=\"color: #000000;\"><b>Uses of serialization and Deserialization in Java<\/b><b>:<\/b><\/span><\/h4>\n<h4 id=\"serialization-and-deserialization-in-java\"><span style=\"color: #000000;\"><strong>Serialization and Deserialization in Java.\u00a0<\/strong><\/span><\/h4>\n<ul>\n<li><b>Serialization<\/b>is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams.<\/li>\n<li>The reverse process of creating object from sequence of bytes is called\u00a0<b>deserialization<\/b>.<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\n<h4 id=\"serialversionuid\"><span style=\"color: #000000;\"><b>serialVersionUID:\u00a0<\/b><\/span><\/h4>\n<ul>\n<li>The serialization runtime associates with each serializable class a version number, called a\u00a0serialVersionUID, which is\u00a0used\u00a0during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.<\/li>\n<li>The serialVersionUID is used as a version control in a Serializable class.<\/li>\n<li>If we do not explicitly declare a serialVersionUID, JVM will do it automatically, based on various aspects of our Serializable class.<\/li>\n<li>Defining a\u00a0serialVersionUID\u00a0field\u00a0in serializable class is\u00a0not mandatory.<\/li>\n<li>If a serializable class has an explicit\u00a0serialVersionUID\u00a0then this field should be of type\u00a0long\u00a0and must be static and final. If there is no\u00a0serialVersionUID\u00a0field\u00a0defined explicitly then serialization runtime will calculate default value for that class. The value can vary based on compiler implementation.<\/li>\n<li>It is advised to use private access modifier for\u00a0serialVersionUID.<\/li>\n<li>Different class can have same\u00a0serialVersionUID.<\/li>\n<li>Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is ignored for array classes.<\/li>\n<li>If there is a difference between serialVersionUID of loaded receiver class and corresponding sender class then\u00a0InvalidClassException\u00a0will be thrown.<\/li>\n<li>we should use different\u00a0serialVersionUID\u00a0for different version of \u00a0same class if we want to forbid serialization of new class with old version of same class.<\/li>\n<\/ul>\n<h4 id=\"serialversionuid-example\"><span style=\"color: #000000;\"><b>SerialVersionUID Example<\/b><\/span><\/h4>\n<ul>\n<li>\u00a0Let start an example to understand how Serializable class use SerialVersionUID to implement version control.<\/li>\n<\/ul>\n<h4 id=\"employee-java\"><span style=\"color: #000000;\"><b>Employee.java<\/b><\/span><\/h4>\n<ul>\n<li>A serializable class with a serialVersionUID of 1L.<\/li>\n<\/ul>\n[pastacode lang=\u201djava\u201d manual=\u201dimport%20java.io.Serializable%3B%0A%0Apublic%20class%20Employee%20implements%20Serializable%0A%7B%0A%0A%09%20%20%20private%20static%20final%20long%20serialVersionUID%20%3D%201L%3B%0A%0A%09%20%20%20String%20name%3B%0A%09%20%20%20String%20age%3B%0A%0A%09%20%20%20public%20void%20setName(String%20name)%0A%7B%0A%09%09%20%20%20this.name%20%3D%20name%3B%0A%7D%0Apublic%20void%20setAge(String%20age)%0A%7B%0A%09%09%20%20%20this.age%20%3D%20age%3B%0A%20%7D%0A%0A%09%20%20%20public%20String%20getName()%0A%7B%0A%09%09%20%20%20return%20this.name%3B%0A%20%7D%0A%0A%09%20%20%20public%20String%20getAge()%0A%7B%0A%09%09%20%20%20return%20this.age%3B%0A%20%7D%0A%0A%09%20%20%20%40Override%0A%09%20%20%20public%20String%20toString()%20%0A%7B%0A%20%20%20%20%09%20%20%20return%20new%20StringBuffer(%22%20Name%20%3A%20%22)%0A%20%20%20%20%09%20%20%20.append(this.name)%0A%20%20%20%20%09%20%20%20.append(%22%20Age%20%3A%20%22)%0A%20%20%20%20%09%20%20%20.append(this.age).toString()%3B%0A%20%7D%0A%7D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h4 id=\"writeobject-java\"><span style=\"color: #000000;\"><b>WriteObject.java<\/b><\/span><\/h4>\n<ul>\n<li>A simple class to write \/ serialize the Employee object into a file \u2013 \u201cc:\\\\employee.ser\u201d<\/li>\n<\/ul>\n[pastacode lang=\u201djava\u201d manual=\u201dimport%20java.io.FileOutputStream%3B%0Aimport%20java.io.ObjectOutputStream%3B%0A%0Apublic%20class%20WriteObject%0A%7B%0A%0A%09public%20static%20void%20main%20(String%20args%5B%5D)%20%0A%7B%0A%0A%09%20%20%20Employee%20employee%20%3D%20new%20Employee()%3B%0A%09%20%20%20employee.setName(%E2%80%9CWikitechy%22)%3B%0A%09%20%20%20employee.setAge(%E2%80%9C25%22)%3B%0A%0A%09%20%20%20try%0A%7B%0AFileOutputStream%20fout%20%3D%20new%20FileOutputStream(%22c%3A%5C%5Cemployee.ser%22)%3B%0AObjectOutputStream%20oos%20%3D%20new%20ObjectOutputStream(fout)%3B%0Aoos.writeObject(employee)%3B%0A%09%09oos.close()%3B%0A%09%09System.out.println(%22Done%22)%3B%0A%0A%7Dcatch(Exception%20ex)%0A%7B%0A%09%09%20%20%20ex.printStackTrace()%3B%0A%20%7D%0A%7D%0A%7D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"readobject-java\"><span style=\"color: #000000;\"><b>ReadObject.java<\/b><\/span><\/h4>\n<ul>\n<li>A simple class to read \/ deserialize the Employee object from file \u2013 \u201cc:\\\\employee.ser\u201d.<\/li>\n<\/ul>\n[pastacode lang=\u201djava\u201d manual=\u201dimport%20java.io.FileInputStream%3B%0Aimport%20java.io.ObjectInputStream%3B%0A%0Apublic%20class%20ReadObject%0A%7B%0Apublic%20static%20void%20main%20(String%20args%5B%5D)%20%7B%0A%0A%09%20%20%20Employee%20employee%3B%0A%0A%20try%0A%7B%0A%0A%09%09%20%20%20FileInputStream%20fin%20%3D%20new%20FileInputStream(%22c%3A%5C%5Cemployee.ser%22)%3B%0A%09%09%20%20%20ObjectInputStream%20ois%20%3D%20new%20ObjectInputStream(fin)%3B%0A%09%09%20%20%20employee%20%3D%20(Employee)%20ois.readObject()%3B%0A%09%09%20%20%20ois.close()%3B%0A%0A%09%09%20%20%20System.out.println(employee)%3B%0A%0A%20%7Dcatch(Exception%20ex)%0A%7B%0A%09%09%20%20%20ex.printStackTrace()%3B%0A%7D%0A%7D%0A%7D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"output\"><span style=\"color: #000000;\"><b>Output:<\/b><\/span><\/h4>\n<p><strong>Name : Wikitechy Age : 25<\/strong><\/p>\n<h4 id=\"default-serialversionuid\"><span style=\"color: #000000;\"><b>D<\/b><b>efault serialVersionUID<\/b><\/span><\/h4>\n<ul>\n<li>\u00a0If no serialVersionUID is declared, JVM will use its own algorithm to generate a default SerialVersionUID.<\/li>\n<li>The default serialVersionUID computation is highly sensitive to class details and may vary from different JVM implementation, and result in an unexpected InvalidClassExceptions during the deserialization process.<\/li>\n<\/ul>\n<h4 id=\"how-to-generate-serialversionuid\"><span style=\"color: #000000;\"><b>How to generate serialVersionUID<\/b><\/span><\/h4>\n<ul>\n<li>we can use JDK \u201cserialver\u201d or Eclipse IDE to generate serialVersionUID automatically<\/li>\n<\/ul>\n<h4 id=\"example\">Example:<\/h4>\n<ul>\n<li>It is easy to understand the exact use of the variable.<\/li>\n<\/ul>\n[pastacode lang=\u201djava\u201d manual=\u201dpackage%20com.jbt%3B%0A%20%0Aimport%20java.io.Serializable%3B%0A%20%0Apublic%20class%20Employee%20implements%20Serializable%0A%7B%0A%20%20%20public%20String%20firstName%3B%0A%20%20%20public%20String%20lastName%3B%0A%20%20%20private%20static%20final%20long%20serialVersionUID%20%3D%205462223600l%3B%0A%7D%0A%20%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h4 id=\"serializaitonclass-java-this-class-will-be-used-to-serialize\"><span style=\"color: #000000;\"><b>SerializaitonClass.java (This class will be used to serialize)<\/b><\/span><\/h4>\n[pastacode lang=\u201djava\u201d manual=\u201dpackage%20com.jbt%3B%0A%20%0Aimport%20java.io.FileOutputStream%3B%0Aimport%20java.io.IOException%3B%0Aimport%20java.io.ObjectOutputStream%3B%0Apublic%20class%20SerializaitonClass%20%7B%0A%20%0A%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20Employee%20emp%20%3D%20new%20Employee()%3B%0A%20emp.firstName%20%3D%20%22wiki%22%3B%0A%20emp.lastName%20%3D%20%22techy%22%3B%0A%20%0A%20try%20%7B%0A%20FileOutputStream%20fileOut%20%3D%20new%20FileOutputStream(%22.%2Femployee.txt%22)%3B%0A%20ObjectOutputStream%20out%20%3D%20new%20ObjectOutputStream(fileOut)%3B%0A%20out.writeObject(emp)%3B%0A%20out.close()%3B%0A%20fileOut.close()%3B%0A%20System.out.printf(%22Serialized%20data%20is%20saved%20in%20.%2Femployee.txt%20file%22)%3B%0A%20%7D%20catch%20(IOException%20i)%20%7B%0A%20i.printStackTrace()%3B%0A%20%7D%0A%20%7D%0A%7D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"deserializationclass-java-this-class-will-be-used-to-deserialize\"><span style=\"color: #000000;\"><b>DeserializationClass.java (This class will be used to <\/b><b>deserialize<\/b><b> )<\/b><\/span><\/h4>\n[pastacode lang=\u201djava\u201d manual=\u201dpackage%20com.jbt%3B%0A%20%0Aimport%20java.io.*%3B%0A%20%0Apublic%20class%20DeserializationClass%20%0A%7B%0A%20public%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0A%20Employee%20emp%20%3D%20null%3B%0A%20try%20%0A%7B%0A%20FileInputStream%20fileIn%20%3D%20new%20FileInputStream(%22.%2Femployee.txt%22)%3B%0A%20ObjectInputStream%20in%20%3D%20new%20ObjectInputStream(fileIn)%3B%0A%20emp%20%3D%20(Employee)%20in.readObject()%3B%0A%20in.close()%3B%0A%20fileIn.close()%3B%0A%20%7D%20catch%20(IOException%20i)%20%0A%7B%0A%20i.printStackTrace()%3B%0A%20return%3B%0A%20%7D%20catch%20(ClassNotFoundException%20c)%20%0A%7B%0ASystem.out.println(%22Employee%20class%20not%20found%22)%3B%0A%20c.printStackTrace()%3B%0A%20return%3B%0A%20%7D%0A%20System.out.println(%22Deserializing%20Employee\u2026%22)%3B%0A%20System.out.println(%22First%20Name%20of%20Employee%3A%20%22%20%2B%20emp.firstName)%3B%0A%20System.out.println(%22Last%20Name%20of%20Employee%3A%20%22%20%2B%20emp.lastName)%3B%0A%20%7D%0A%7D%0A%20%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Now execute \u201cSerializationClass.java\u201d and then \u201cDeserializationClass.java\u201d. It will first create a file with Employee object\u2019s state and then while de-serialization it creates object from the same file.<\/li>\n<\/ul>\n<h4 id=\"output-2\"><span style=\"color: #000000;\"><b>Output:<\/b><\/span><\/h4>\n<blockquote><p><strong>Serialized data is saved in .\/employee.txt file<\/strong><\/p>\n<p><strong>Deserializing Employee\u2026<\/strong><\/p>\n<p><strong>First Name of Employee: wiki<\/strong><\/p>\n<p><strong>Last Name of Employee: techy<\/strong><\/p><\/blockquote>\n<ul>\n<li>Remove \u201cserialVersionUID\u201d variable from Employee.java file and again run \u201cSerializationClass.java\u201d\u00a0 file.<\/li>\n<li>It will create \u201cemployee.txt\u201d\u00a0 file again with\u00a0 the object\u2019s state .<\/li>\n<li>Now let\u2019s add a new variable in Employee class\u00a0 suppose\u00a0 String Address.<\/li>\n<\/ul>\n<h4 id=\"employee-java-2\"><span style=\"color: #000000;\"><b>Employee.java<\/b><\/span><\/h4>\n[pastacode lang=\u201djava\u201d manual=\u201dpackage%20com.jbt%3B%0A%20%0Aimport%20java.io.Serializable%3B%0A%20%0Apublic%20class%20Employee%20implements%20Serializable%0A%7B%0A%20%20%20public%20String%20firstName%3B%0A%20%20%20public%20String%20lastName%3B%0A%20%20%20public%20String%20Address%3B%0A%20%20%20%2F%2FVariable%20is%20commented%20%0A%2F%2F%20%20%20private%20static%20final%20long%20serialVersionUID%20%3D%205462223600l%3B%0A%7D%0A%20%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Now run \u201cDeserializationClass.java\u201d<\/p>\n[ad type=\u201dbanner\u201d]\n<h4 id=\"output-3\"><span style=\"color: #000000;\">Output:<\/span><\/h4>\n<blockquote><p><strong>java.io.InvalidClassException: com.jbt.Employee; local class incompatible: stream classdesc serialVersionUID = 5462223600, local class serialVersionUID = -3607530122250644586<\/strong><\/p>\n<p><strong>at java.io.ObjectStreamClass.initNonProxy(Unknown Source)<\/strong><\/p>\n<p><strong>at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)<\/strong><\/p>\n<p><strong>at java.io.ObjectInputStream.readClassDesc(Unknown Source)<\/strong><\/p>\n<p><strong>at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)<\/strong><\/p>\n<p><strong>at java.io.ObjectInputStream.readObject0(Unknown Source)<\/strong><\/p>\n<p><strong>at java.io.ObjectInputStream.readObject(Unknown Source)<\/strong><\/p>\n<p><strong>at com.jbt.DeserializationClass.main(DeserializationClass.java:11)<\/strong><\/p><\/blockquote>\n<ul>\n<li>It will throw an incompatible exception.<\/li>\n<li>Because the given class Employee.java was changed in between serialization and de-serialization process.<\/li>\n<li>Hence the system failed to identify that it is still the same class.<\/li>\n<li>To make our system understand that it is the same class we have to make use of\u00a0<b><i>serialVersionUID<\/i><\/b> variable inside class.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>SERIALIZATION: To\u00a0serializean object means to convert its state to a byte stream so that the byte stream can be returned back into a copy of the object. A\u00a0Javaobject is\u00a0serializable if its class or any of its super classes implements either the\u00a0java.io.Serializable interface or its subinterface,\u00a0java.io.Externalizable. DESERIALIZATION: The reverse process of creating object from sequence of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139],"tags":[2920,2919,2917,2902,2911,2913,2915,2906,2905,2907,2912,2903,2904,2908,2910,2177,2916,2918,2909,2914],"class_list":["post-1468","post","type-post","status-publish","format-standard","hentry","category-java","tag-and-of-type-long","tag-final","tag-how-serialversionuid-is-helpful-in-serialization-java","tag-how-to-generate-serialversionuid","tag-isnt-suppressing-warnings-better-option-than-adding-serialversionuid-in-this-scenario","tag-java-why-does-my-object-instance-have-two-serialversionuids","tag-serialized-lambda-and-no-serialversionuid","tag-serialversionuid-1l-means","tag-serialversionuid-eclipse","tag-serialversionuid-exception","tag-serialversionuid-field-warning-in-eclipse","tag-serialversionuid-intellij","tag-what-is-used-to-associate-the-serialized-output-with-the-class-used-in-the-serialization-process","tag-which-method-is-used-to-combine-two-paths-in-java","tag-why-generate-long-serialversionuid-instead-of-a-simple-1l","tag-why-is-it-faster-to-process-a-sorted-array-than-an-unsorted-array","tag-why-java-declared-serialversionuid-in-exception-and-throwable-class","tag-why-serialversionuid-in-java-must-be-static","tag-why-serialversionuid-is-static-in-java","tag-why-user-defined-serialversionuid-is-not-used-while-desialization"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1468"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1468\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}