{"id":2061,"date":"2011-04-20T17:23:06","date_gmt":"2011-04-20T11:53:06","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2061"},"modified":"2011-04-20T17:23:06","modified_gmt":"2011-04-20T11:53:06","slug":"explain-serialversionuid-in-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/explain-serialversionuid-in-java\/","title":{"rendered":"Explain serialVersionUID in Java"},"content":{"rendered":"<p>The <a title=\"Serialization Tutorial\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/serialization-marshalling-deflating-in-java\/\">serialization <\/a>runtime associates with each serializable class a version number, called a <strong>serialVersionUID<\/strong>, which is used during 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. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender&#8217;s class, then deserialization will result in an <strong>InvalidClassException<\/strong>. A serializable class can declare its own serialVersionUID explicitly by declaring a field named &#8220;<strong>serialVersionUID<\/strong>&#8221; that must be static, final, and of type long:<\/p>\n<blockquote><p><strong>ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;<\/strong><\/p><\/blockquote>\n<p>If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly <span style=\"text-decoration: underline;\">recommended that all serializable classes explicitly declare serialVersionUID values, since the<\/span><em><span style=\"text-decoration: underline;\"> default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization<\/span>.<\/em> <strong>Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value<\/strong>. It is also strongly advised that explicit serialVersionUID declarations <strong>use the private modifier<\/strong> where possible, since such declarations apply only to the immediately declaring class.serialVersionUID fields are not useful as inherited members.<\/p>\n<p><!--more-->Example demonstrating the use of serialVersionUID :<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Serialization;\n\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.io.ObjectOutputStream;\nimport java.io.Serializable;\n\nclass Demo1 implements Serializable {\n\n\tpublic Demo1() {\n\t}\n}\n\npublic class ExternizationDemo {\n\tpublic static void main(String&#x5B;] args) throws IOException, ClassNotFoundException {\n\t\tDemo1 b1 = new Demo1();\n\n\t\tObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(&quot;Demo1.srz&quot;));\n\t\to.writeObject(b1);\n\t\to.close();\n\n\t}\n}\n<\/pre>\n<p>Above program will run perfectly. now add one more parameter in above class and lets try below example.<\/p>\n<pre class=\"brush: java; highlight: [9]; title: ; notranslate\" title=\"\">\npackage com.G2.Serialization;\n\nimport java.io.FileInputStream;\nimport java.io.IOException;\nimport java.io.ObjectInputStream;\nimport java.io.Serializable;\n\nclass Demo1 implements Serializable {\n\tString NeedOfSerial = &quot;Very imp&quot;;\n\tpublic Demo1() {\n\t}\n}\n\npublic class ExternizationDemo {\n\tpublic static void main(String&#x5B;] args) throws IOException, ClassNotFoundException {\n\t\tDemo1 b1 = new Demo1();\n\n\t\t ObjectInputStream in = new ObjectInputStream(new\n\t\t FileInputStream(&quot;Demo1.srz&quot;));\n\t\t b1 = (Demo1) in.readObject();\n\t\t System.out.println(&quot; --- Object recovered --- &quot;);\n\n\t}\n}\n<\/pre>\n<p>Output will be something like below message :<\/p>\n<blockquote><p>Exception in thread &#8220;main&#8221; java.io.InvalidClassException: com.G2.Serialization.Demo1; local class incompatible: stream classdesc serialVersionUID = -9110957255177458835, local class serialVersionUID = 4792304913344431293<br \/>\nat java.io.ObjectStreamClass.initNonProxy(Unknown Source)<br \/>\nat java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)<br \/>\nat java.io.ObjectInputStream.readClassDesc(Unknown Source)<br \/>\nat java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)<br \/>\nat java.io.ObjectInputStream.readObject0(Unknown Source)<br \/>\nat java.io.ObjectInputStream.readObject(Unknown Source)<br \/>\nat com.G2.Serialization.ExternizationDemo.main(ExternizationDemo.java:26)<\/p><\/blockquote>\n<p>By adding below line in class Demo1, above problem will be resolved.<\/p>\n<blockquote><p><strong>private static final long serialVersionUID = 998985689L;<\/strong><\/p><\/blockquote>\n<p>value of serialVersionUID can be anything unique to the class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is serialVersionUID and what is its need ?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[329],"class_list":["post-2061","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1612,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/serialization-marshalling-deflating-in-java\/","url_meta":{"origin":2061,"position":0},"title":"Serialization \/ Marshalling \/ Deflating in JAVA","author":"Jitendra","date":"March 9, 2011","format":false,"excerpt":"Concept of Serialization \/ Marshalling \/ Deflating in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Serialization ,Marshalling ,deflating, DeSerialization ,UnMarshalling ,inflating","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Serialization-Marshalling-deflating-DeSerialization-UnMarshalling-inflating.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2049,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/explain-externalizable-in-java\/","url_meta":{"origin":2061,"position":1},"title":"Explain Externalizable in Java","author":"Jitendra","date":"April 20, 2011","format":false,"excerpt":"Explain Externalizable in Java","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2017,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-j2ee-interview-questions-1\/","url_meta":{"origin":2061,"position":2},"title":"Java &#8211; J2EE Interview Questions &#8211; 1","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"JAVA - J2EE Interview Questions - 1, JAVA - J2EE Interview Questions - 1,custom tag JSP, Externalization , serialVersionUID, difference between interface and abstract class, iterate HashMap","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3050,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/my-favorite-top-10-features-of-winter-13-release-salesforce\/","url_meta":{"origin":2061,"position":3},"title":"My Favorite Top 10 Features of Winter 13 release &#8211; Salesforce","author":"Jitendra","date":"August 24, 2012","format":false,"excerpt":"Dear Friends, I am very excited to write this article about the cool Winter 13 features which i have added in My List. There are lots of lots of new features added in this release and i have made the list of my top 10 favorite features. Yesterday Salesforce published\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Winter 13 Release Notes","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Salesforce-Winter-13-Release-Notes-300x212.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/interface\/","url_meta":{"origin":2061,"position":4},"title":"Interface in JAVA","author":"Jitendra","date":"August 2, 2010","format":false,"excerpt":"Explains the usage and benefits of interface in JAVA.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3411,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","url_meta":{"origin":2061,"position":5},"title":"Apex Interview Question \u2013 Salesforce &#8211; Part 16","author":"Jitendra","date":"July 28, 2013","format":false,"excerpt":"151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ? Ans : It can be done with help of External Id. [java] Date dt = Date.today().addDays(7); Opportunity newOpportunity = new Opportunity(Name = 'shivasoft', StageName = 'Prospecting',\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2061"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2061\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}