Serialize static variable in java 2013

View previous topic View next topic Go down

Serialize static variable in java 2013

Post by Admin on Sun Sep 15, 2013 4:03 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">Can we serialize static variable in java?</br> No. Since static members are associated with the class and they do not belong to the individual objects, they are not serialized. Static field values will be reinitialized to whatever value they are set to when the class is loaded.</br>Consider a below class with a static field with initial value as 10.</br></br><b>Person.java</b></br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.Serializable;<br /> public class Person implements Serializable { <br /> private static final long serialVersionUID = -8935100740005343248L;<br /> static int staticField = 10; <br /> private String firstName; <br /> private String lastName; <br /> public Person(String firstName, String lastName) { <br /> super(); <br /> this.firstName = firstName; <br /> this.lastName = lastName; <br /> } <br /> public String getFirstName() { <br /> return firstName; <br /> } <br /> public void setFirstName(String firstName) { <br /> this.firstName = firstName; <br /> } <br /> public String getLastName() { <br /> return lastName; <br /> } <br /> public void setLastName(String lastName) { <br /> this.lastName = lastName; <br /> } <br /> } <br /></code></pre> <b>SerializationMain.java - In this class, update the static field value to 20 as shown below and then serialize the class</b><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.FileOutputStream; <br /> import java.io.IOException; <br /> import java.io.ObjectOutputStream; <br /> public class SerializationMain { <br /> public static void main(String[] args) throws IOException { <br /> Person person = new Person("Kumar", "Bhatia"); <br /> // Update static field to some value and then serialize<br /> Person.staticField = 20;<br /> System.out.println("person " + person); <br /> FileOutputStream fos = new FileOutputStream("serial.txt"); <br /> ObjectOutputStream oos = new ObjectOutputStream(fos); <br /> oos.writeObject(person); <br /> oos.flush(); <br /> oos.close(); <br /> } <br /> } <br /></code></pre> <b>DeSerializationMain.java</b> - Once we de-serialize the Person object, the value of staticField will be initialized to 10 as shown below but not 20 as done in SerializationMain.java <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.FileInputStream; <br /> import java.io.IOException; <br /> import java.io.ObjectInputStream; <br /> public class DeSerializationMain { <br /> public static void main(String[] args) throws IOException, ClassNotFoundException { <br /> FileInputStream fis = new FileInputStream("serial.txt"); <br /> ObjectInputStream ois = new ObjectInputStream(fis); <br /> Person person = (Person) ois.readObject(); <br /> ois.close(); <br /> System.out.println("person " + person.staticField); <br /> } <br /> } <br /></code></pre></br><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-HqukQZIDenU/Ue-rPgNBZHI/AAAAAAAAAFw/3T9KUtP3gtI/s1600/FileStructure_Serialization.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-HqukQZIDenU/Ue-rPgNBZHI/AAAAAAAAAFw/3T9KUtP3gtI/s320/FileStructure_Serialization.JPG" /></a></div></br></br></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">Can we serialize static variable in java?</br> No. Since static members are associated with the class and they do not belong to the individual objects, they are not serialized. Static field values will be reinitialized to whatever value they are set to when the class is loaded.</br>Consider a below class with a static field with initial value as 10.</br></br><b>Person.java</b></br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.Serializable;<br /> public class Person implements Serializable { <br /> private static final long serialVersionUID = -8935100740005343248L;<br /> static int staticField = 10; <br /> private String firstName; <br /> private String lastName; <br /> public Person(String firstName, String lastName) { <br /> super(); <br /> this.firstName = firstName; <br /> this.lastName = lastName; <br /> } <br /> public String getFirstName() { <br /> return firstName; <br /> } <br /> public void setFirstName(String firstName) { <br /> this.firstName = firstName; <br /> } <br /> public String getLastName() { <br /> return lastName; <br /> } <br /> public void setLastName(String lastName) { <br /> this.lastName = lastName; <br /> } <br /> } <br /></code></pre> <b>SerializationMain.java - In this class, update the static field value to 20 as shown below and then serialize the class</b><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.FileOutputStream; <br /> import java.io.IOException; <br /> import java.io.ObjectOutputStream; <br /> public class SerializationMain { <br /> public static void main(String[] args) throws IOException { <br /> Person person = new Person("Kumar", "Bhatia"); <br /> // Update static field to some value and then serialize<br /> Person.staticField = 20;<br /> System.out.println("person " + person); <br /> FileOutputStream fos = new FileOutputStream("serial.txt"); <br /> ObjectOutputStream oos = new ObjectOutputStream(fos); <br /> oos.writeObject(person); <br /> oos.flush(); <br /> oos.close(); <br /> } <br /> } <br /></code></pre> <b>DeSerializationMain.java</b> - Once we de-serialize the Person object, the value of staticField will be initialized to 10 as shown below but not 20 as done in SerializationMain.java <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.FileInputStream; <br /> import java.io.IOException; <br /> import java.io.ObjectInputStream; <br /> public class DeSerializationMain { <br /> public static void main(String[] args) throws IOException, ClassNotFoundException { <br /> FileInputStream fis = new FileInputStream("serial.txt"); <br /> ObjectInputStream ois = new ObjectInputStream(fis); <br /> Person person = (Person) ois.readObject(); <br /> ois.close(); <br /> System.out.println("person " + person.staticField); <br /> } <br /> } <br /></code></pre></br><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-HqukQZIDenU/Ue-rPgNBZHI/AAAAAAAAAFw/3T9KUtP3gtI/s1600/FileStructure_Serialization.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-HqukQZIDenU/Ue-rPgNBZHI/AAAAAAAAAFw/3T9KUtP3gtI/s320/FileStructure_Serialization.JPG" /></a></div></br></br></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum