{"id":2049,"date":"2011-04-20T09:12:49","date_gmt":"2011-04-20T03:42:49","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2049"},"modified":"2011-04-20T09:12:49","modified_gmt":"2011-04-20T03:42:49","slug":"explain-externalizable-in-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/explain-externalizable-in-java\/","title":{"rendered":"Explain Externalizable in Java"},"content":{"rendered":"<p>We have seen an example of Serialization and how it works in our <a title=\"Serialization in Java\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/serialization-marshalling-deflating-in-java\/\" target=\"_blank\">previous <\/a>article. However there is one more interface provided by the Java API which is &#8220;<a title=\"Externalizable API\" rel=\"nofollow\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/io\/Externalizable.html\" target=\"_blank\">Externalizable<\/a>&#8220;. This interface extends &#8220;Serializable&#8221; and provides two more methods as shown below :<\/p>\n<ol>\n<li> void readExternal(<a title=\"ObjectInput API\" rel=\"nofollow\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/io\/ObjectInput.html\" target=\"_blank\">ObjectInput <\/a>in)<\/li>\n<li> void writeExternal(<a title=\"ObjectOutput API\" rel=\"nofollow\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/io\/ObjectOutput.html\" target=\"_blank\">ObjectOutput <\/a>out)<\/li>\n<\/ol>\n<p><strong><!--more-->Externalizable interface (from API):<\/strong><\/p>\n<p><strong>Only the identity<\/strong> of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods.<br \/>\nObject Serialization uses the Serializable and Externalizable interfaces. Object persistence mechanisms can use them as well. Each object to be stored is tested for the Externalizable interface. <em>If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream.<\/em><br \/>\nWhen an Externalizable object is reconstructed, <span style=\"text-decoration: underline;\"><strong>an instance is created using the public no-arg constructor<\/strong><\/span>, then the readExternal method called. Serializable objects are restored by reading them from an ObjectInputStream.<br \/>\nAn Externalizable instance can designate a substitution object via the writeReplace and readResolve methods documented in the Serializable interface.<\/p>\n<p>Example :<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Serialization;\n\nimport java.io.Externalizable;\nimport java.io.FileInputStream;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.io.ObjectInput;\nimport java.io.ObjectInputStream;\nimport java.io.ObjectOutput;\nimport java.io.ObjectOutputStream;\nimport java.io.Serializable;\n\nclass Demo1 implements Externalizable {\n\tpublic Demo1() {\n\t\tSystem.out.println(&quot;Demo1 Constructor&quot;);\n\t}\n\n\tpublic void writeExternal(ObjectOutput out) throws IOException {\n\t\tSystem.out.println(&quot;Demo1 writeExternal()&quot;);\n\t}\n\n\tpublic void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {\n\t\tSystem.out.println(&quot;Demo1 readExternal()&quot;);\n\t}\n}\n\nclass Demo2 implements Externalizable {\n\tpublic Demo2() {\n\t\tSystem.out.println(&quot;Demo2 Constructor&quot;);\n\t}\n\n\tpublic void writeExternal(ObjectOutput out) throws IOException {\n\t\tSystem.out.println(&quot;Demo2 writeExternal()&quot;);\n\t}\n\n\tpublic void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {\n\t\tSystem.out.println(&quot;Demo2 readExternal()&quot;);\n\t}\n}\n\npublic class ExternizationDemo {\n\t\/\/ Throw exceptions to console:\n\tpublic static void main(String&#x5B;] args) throws IOException, ClassNotFoundException {\n\t\tSystem.out.println(&quot;--- GOing to create objects ---&quot;);\n\t\tDemo1 b1 = new Demo1();\n\t\tDemo2 b2 = new Demo2();\n\t\tObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(&quot;Demo1.out&quot;));\n\t\tSystem.out.println(&quot;--- Saving objects ---&quot;);\n\t\to.writeObject(b1);\n\t\to.writeObject(b2);\n\t\to.close();\n\n\t\t\/\/ Get back objects\n\t\tObjectInputStream in = new ObjectInputStream(new FileInputStream(&quot;Demo1.out&quot;));\n\t\tSystem.out.println(&quot; --- Recovering b1 --- &quot;);\n\t\tb1 = (Demo1) in.readObject();\n\t\tSystem.out.println(&quot; --- Object recovered --- &quot;);\n\t\tSystem.out.println(&quot; --- Recovering b2 --- &quot;);\n\t\tb2 = (Demo2)in.readObject();\n\t}\n}\n<\/pre>\n<p>Output :<\/p>\n<blockquote><p>&#8212; GOing to create objects &#8212;<br \/>\nDemo1 Constructor<br \/>\nDemo2 Constructor<br \/>\n&#8212; Saving objects &#8212;<br \/>\nDemo1 writeExternal()<br \/>\nDemo2 writeExternal()<br \/>\n&#8212; Recovering b1 &#8212;<br \/>\nDemo1 Constructor<br \/>\nDemo1 readExternal()<br \/>\n&#8212; Object recovered &#8212;<br \/>\n&#8212; Recovering b2 &#8212;<br \/>\nDemo2 Constructor<br \/>\nDemo2 readExternal()<\/p><\/blockquote>\n<p>O\/p of same program when  class implements serializable interface.<\/p>\n<blockquote><p>&#8212; GOing to create objects &#8212;<br \/>\nDemo1 Constructor<br \/>\nDemo2 Constructor<br \/>\n&#8212; Saving objects &#8212;<br \/>\n&#8212; Recovering b1 &#8212;<br \/>\n&#8212; Object recovered &#8212;<br \/>\n&#8212; Recovering b2 &#8212;<\/p><\/blockquote>\n<p><strong>As we can see, in case of serialization object is readed from ObjectInputStream whereas in case externalization, default no args constructor is called (Object is created).<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explain Externalizable in Java<\/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-2049","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1820,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-threading-executor-framework-and-callable-interface\/","url_meta":{"origin":2049,"position":0},"title":"Java Threading &#8211; Executor Framework and Callable Interface","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Java Threading - Executor Framework and Callable Interface with example","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":700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/interface\/","url_meta":{"origin":2049,"position":1},"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":702,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-interfaceinheritance-abstract-class\/","url_meta":{"origin":2049,"position":2},"title":"Interface, Inheritance and abstract class","author":"Jitendra","date":"August 6, 2010","format":false,"excerpt":"What is abstract class, interface and inheritance in java. Source code and example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Inheritance, Interface and abstract class","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/Inheritance1-300x173.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":757,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/anonymous-classimplements-interface-methods-of-threads\/","url_meta":{"origin":2049,"position":3},"title":"Create Thread using Anonymous class and Interface","author":"Jitendra","date":"August 16, 2010","format":false,"excerpt":"Demonstration of creating Thread by Anonymous class and Interface","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":1612,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/serialization-marshalling-deflating-in-java\/","url_meta":{"origin":2049,"position":4},"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":1683,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/reading-and-writing-custom-annotation\/","url_meta":{"origin":2049,"position":5},"title":"Reading and Writing Custom Annotation","author":"Jitendra","date":"March 16, 2011","format":false,"excerpt":"Article on Reading and Writing Custom Annotation with example","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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2049","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=2049"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2049\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}