How can I deserialize a class which was modified after serialization?
More specifically, I know this can be done when a class had serialVersionUID in its initial version. Is there any way to do it for classes without serialVersionUID?
I have an object
package com.test.serialize;
import java.io.Serializable;
public class MyObject implements Serializable{
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and I serialize classes like this
package com.test.serialize;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializeTest {
public static void main(String[] args) {
try {
MyObject myObject = new MyObject();
myObject.setName("Ajit");
ObjectOutputStream objectOStr = null;
ByteArrayOutputStream byteOStr = null;
try {
byteOStr = new ByteArrayOutputStream();
objectOStr = new ObjectOutputStream(byteOStr);
objectOStr.writeObject(myObject);
} catch (IOException e) {
System.out.println(e);
} finally {
try {
if (objectOStr != null)
objectOStr.close();
} catch (IOException e2) {
}
}
FileOutputStream fo = new FileOutputStream(new File("serialize"));
fo.write(byteOStr.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and deserialize like this
package com.test.serialize;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.RandomAccessFile;
public class DeserializeTest {
public static void main(String[] args) {
try {
// File f = new File("serialize");
// FileInputStream fs = new FileInputStream(f);
RandomAccessFile raF = new RandomAccessFile("serialize", "r");
byte[] b = new byte[(int)raF.length()];
raF.read(b);
ObjectInputStream oIstream = null;
ByteArrayInputStream bIstream = null;
bIstream = new ByteArrayInputStream(b);
oIstream = new ObjectInputStream(bIstream);
Object finalResult = oIstream.readObject();
System.out.println(finalResult.toString());
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
After some time, I added
#Override
public String toString() {
return "MyObject [name=" + name + ", names=" + names + "]";
}
to MyObject. After adding that I got exceptions like
java.io.InvalidClassException: com.test.serialize.MyObject; local class in
compatible: stream classdesc serialVersionUID = 5512234731442983181, local class
serialVersionUID = -6186454222601982895
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:617)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at com.test.serialize.DeserializeTest.main(DeserializeTest.java:25)
Please help me with this.
Thanks #Gábor Bakos.
This can be solved by creating serialVersionUID for older class (Which signatures should be same as the one during serialization )and adding that serialVersionUID in current class.
serialver -classpath /***PATH***/bin com.test.serialize.MyObject
That returns
com.test.serialize.MyObject: static final long serialVersionUID = 5512234731442983181L;
After that I have added it to my MyObject as below
package com.test.serialize;
import java.io.Serializable;
public class MyObject implements Serializable{
/**
* Added serial version Id of old class, created before adding new fields
*/
private static final long serialVersionUID = 5512234731442983181L;
public MyObject() {
System.out.println("Constructor");
}
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String names ="Altered after change!";
public String getNames() {
return names;
}
public void setNames(String names) {
System.out.println("Setting names");
this.names = names;
}
#Override
public String toString() {
return "MyObject [name=" + name + ", names=" + names + "]";
}
}
It works fine.
More info refer: serialver
First advice: use serialization, because everything is almost done.
Second advice: use a serialVersionUID and keep it fix with one version: it is here to warn you and prevent confusion between different serialized versions.
So : if you change fields or meaning of fields, change the serialVersionUID.
Then you have your backward compatibility problem.
See this for many ideas: Managing several versions of serialized Java objects
IMHO:
whatever solution you take, keep in mind that your program will be managing objects with partial datas: then you have to manage all cases with or without datas.
if you dont change often your version: use several different classes. Perhaps subclasses, or implementations of an interface: then you can get your program, and you manage several versions of object: MyClass_V1, MyClass_V2, etc. When you deserialize, you can test/retry and get the good Object. After that, you perhaps have to convert datas between your classes
if you change your version, by adding new fields (not changing old fields), it is a little more easy (subclasses, converting is direct to parents)
or you could consider use a XML structure to serialize and deserialize: you can have backward and forward compatibility because it is extensible: fields are there, or are null. You have to manage mapping yourself or use some libraries.
Hope it helps !
I would remember following points,
Every Serializable class contains a serialVersionUID ( it doesn't matter if you have specified the one explicitly or not ).
There are compatible changes and there are incompatible changes
e.g. adding a new field is a compatible change, removing a field is not a compatible change. Adding / removing / editing a method are generally compatible changes but in your case surely that is not the way it is ( serialVersionUID got changed after you added toString() method)
3.Prior to modify the class, you can use serialver utility to find serialVersionUID of old class and use that in new class
Don't think there are any other magic tricks :)
Related
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 3 years ago.
Can someone help me to serialze an object into a string? The result of my code is a bit weird, I need to get a toString(); methode or something with which i can serialze an object into a string but I dont know any.
Thanks for the help
results with getString -> only "null"
result without getString(); -> Fruit#4dd8dc3, Fruit#6d03e736, Fruit#568db2f2, Fruit#378bf509, Fruit#5fd0d5ae,
Fruit#2d98a335, Fruit#16b98e56, Fruit#7ef20235"
import java.io.Serializable;
public class Fruit implements Comparable<Fruit>,Serializable{
String getString;
String name;
int gewicht;
public String getString() {
return this.getString;
}
public Fruit(String name, int gewicht) {
this.name=name;
this.gewicht=gewicht;
}
public int compareTo(Fruit otherFruit) {
if(this.gewicht < otherFruit.gewicht)
return -1;
if(this.gewicht>otherFruit.gewicht)
return 1;
return 0;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.TreeSet;
public class FruitTree {
public static void main(String[] args) {
TreeSet<Fruit> fruitTree = new TreeSet<Fruit>();
fruitTree.add(new Fruit("Kiwi",5));
fruitTree.add(new Fruit("Kirsche",1));
fruitTree.add(new Fruit("Ananas",75));
fruitTree.add(new Fruit("Zitrone",15));
fruitTree.add(new Fruit("Grapefruit",44));
fruitTree.add(new Fruit("Banane",55));
fruitTree.add(new Fruit("Kirsche",2));
fruitTree.add(new Fruit("Kiwi",8));
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("IO.txt"));
Iterator<Fruit> it = fruitTree.iterator();
while(it.hasNext()) {
oos.writeObject(it.next());
}
oos.writeObject(null);
ObjectInputStream ois = new ObjectInputStream (new FileInputStream("IO.txt"));
Object readObject=null;
TreeSet<Fruit> deserializedFruits= new TreeSet<Fruit>();
do {
readObject=ois.readObject();
if(readObject !=null)
deserializedFruits.add((Fruit) readObject);
}
while (readObject!=null);
it=deserializedFruits.iterator();
while(it.hasNext()) {
System.out.println(it.next().getString());
ois.close();
oos.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Can someone help me to serialze an object into a string? The result of my code is a bit weird, I need to get a toString(); methode or something with which i can serialze an object into a string but I dont know any.
Thanks for the help
Adding implements Serializable to your class will not override default Object.toString implementation - that's not how Java works (btw this default implementation will actually provide you what you got - for example Fruit#5fd0d5ae - that's how it works)
what you need to do is to override toString by yourself - for example
public class Fruit implements Comparable<Fruit>,Serializable {
// ...
#Override
public String toString() {
return "name: " + this.name + ", gewicht: " + String.valueOf(this.gewicht);
}
}
or to use some existing tool that will generate this method for you (like lombok), or even better that will allow you to serialize your class object to some common format like JSON or XML (for this take a look at gson or Jackson)
What happens during serialization in java, if two object refrences are pointing to the same serializable Object? Does the Serializable Objects get saved twice ?
for example :
class King implements java.io.Serializable {
private String name="Akbar";
}
class Kingdom implements java.io.Serializable {
King goodKing=new King();
King badKing=goodKing;
}
public class TestSerialization {
public static void serializeObject(String outputFileName,
Object serializableObject) throws IOException {
FileOutputStream fileStream=new FileOutputStream(outputFileName);
ObjectOutputStream outStream=new ObjectOutputStream(fileStream);
outStream.writeObject(serializableObject);
outStream.close();
}
public static void main(String[] args) {
Kingdom kingdom=new Kingdom();
try {
TestSerialization.serializeObject("Kingdom1.out", kingdom);
}catch(IOException ex) {
ex.getMessage();
}
}
}
Now, whether only one object state is saved for both goodKing and badKing refrences or the King object get saved twice ?
The documentation for ObjectOutputStream says what happens:
The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.
(My emphasis)
E.g., if you have multiple references to a single object, when the graph is reconstituted, you end up with multiple references to a single reconstituted version of that object, not references to multiple equivalent instances of it.
Of course, if the container being serialized implements a different mechanism, the behavior is dictated by that mechanism, not the default one.
So for instance, if we have Thing and Test:
Thing.java:
import java.io.*;
import java.util.*;
public class Thing implements Serializable {
private Map<String,String> map1;
private Map<String,String> map2;
public Thing() {
this.map1 = new HashMap();
this.map2 = this.map1; // Referring to same object
}
public void put(String key, String value) {
this.map1.put(key, value);
}
public boolean mapsAreSameObject() {
return this.map1 == this.map2;
}
}
Test.java:
import java.io.*;
public class Test implements Serializable {
public static final void main(String[] args) {
try
{
// Create a Thing
Thing t = new Thing();
t.put("foo", "bar");
// Write it out
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("foo"));
os.writeObject(t);
os.close();
os = null;
// Read it in
Thing t2;
ObjectInputStream is = new ObjectInputStream(new FileInputStream("foo"));
t2 = (Thing)is.readObject();
is.close();
is = null;
// Same underlying map for both properties?
System.out.println("t2.mapsAreSameObject? " + t2.mapsAreSameObject());
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
}
}
And run java Test, we get:
t2.mapsAreSameObject? true
...because both of Thing's members, map1 and map2 end up pointing to a single HashMap instance.
How I can store a value in an array variable say x.
At every instance, whenever we have a value for x or when we don't have a value for x, we can have a time. Like at instance 1 millisec, x has value 245 and at 3 millisec, x has value 300.
How do I do this in Java without using or storing values in a Database?
It's hard to know what you are doing based on the description, but if your intention is to store a piece of intelligence (some integer value, say) with a corresponding monotonically increasing value such as a timestamp in millis, you should use a data structure or object to do so. I often refer to such a fundamenal data piece as an "atom".
If you will never need a more complicated atom than a long - int relationship, you could use some sort of Map.
If you ever intend to make this atom more complicated, you might want to create your own object. The nice thing about this way is that you can build in error handling (e.g., time not increasing monotonically, or apparent gaps). It also makes it a little more straightforward when persisting/caching this data to some store so you can work on datasets larger than memory. This last part is probably the trickiest part of your design.
#Arunroy in base on my understanding of posible question i have a example i wrote. It possibly could help you, it has a class that implements an interface Serializable that enable the class to be serializable in other words to be stored in a file and then be read. This process is called serialization. The class creates a file and store the class inside the file. Then with the deserialization process you can read it.
The code:
package storage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Timestamp;
public class MyValues{
public static void main(String[] args) {
java.util.Date date = new java.util.Date();
MyData d = new MyData(new Timestamp(date.getTime()));
System.out.println(""+d.tiempo);
serialize(d);
deSerialize();
}
public static boolean serialize(MyData a) {
try {
FileOutputStream fileOut
= new FileOutputStream("c:\\myfolder1\\mydata.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(a);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in c:\\myfolder1\\mydata.ser \n");
} catch (IOException i) {
i.printStackTrace();
}
return false;
}
public static void deSerialize() {
MyData a = null;
try {
FileInputStream fileIn = new FileInputStream("c:\\myfolder1\\employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
a = (MyData) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized MyData...");
System.out.println("TimeStamp: " + a.tiempo);
return;
}
}
class MyData implements Serializable{
public Timestamp tiempo;
public MyData(Timestamp tiempo) {
this.tiempo = tiempo;
}
}
Trying to serialize and send Lot object to socket. Getting error:
java.io.NotSerializableException: com.server.ClientServiceThread
Why?
public class ClientServiceThread extends Thread {... // form here called sendObj ...}
public class FlattenLot {
public void sendObj(){
try {
out = new ObjectOutputStream(oStream);
out.writeObject(lot); // error
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Lot class:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.Calendar;
public class Lot implements Serializable{
private static final long serialVersionUID = 1L;
public ArrayList<ClientServiceThread> clientBidsLog = new ArrayList<ClientServiceThread>();
public ArrayList<Integer> bidLog = new ArrayList<Integer>();
private List<Integer> bids = new ArrayList<Integer>();
private List<ClientServiceThread> clients = new ArrayList<ClientServiceThread>();
private String NAME;
private int INITIAL_PRICE;
private int MAX_BID = 0;
public volatile boolean notAvailable = false;
Lot(String name, int initPrice){
NAME = name;
INITIAL_PRICE = initPrice;
}
public synchronized String getName(){return NAME;}
public synchronized int getInitPrice(){return INITIAL_PRICE;}
public synchronized void subscribe(ClientServiceThread t){
clients.add(t);
}
public synchronized void unsubscribe(ClientServiceThread t){
clients.remove(t);
}
public synchronized boolean makeBid(ClientServiceThread t,int i){
if(i > INITIAL_PRICE && i > MAX_BID){
clientBidsLog.add(t);
bidLog.add(i);
bids.add(i);
MAX_BID = i;
t.LAST_BID = i;
notifyAllSubscribers("New bid: "+this.getMaxBid()+" made by "+this.clientBidsLog.get(this.clientBidsLog.size()-1).CLIENT_NAME);
return true;
}else{
return false;
}
}
public synchronized void notifyAllSubscribers(String msg){
for (ClientServiceThread client : clients){
client.lotUpdated(this, msg);
}
}
public synchronized int getMaxBid(){return MAX_BID;}
private Date time;
public Lot() {
time = Calendar.getInstance().getTime();
}
public Date getTime() {
return time;
}
}
The error is caused by trying to serialize a ClientServiceThread, which is not serializable. Somehow one of those is part of a Lot. If Lot is not declared with a ClientServiceThread field (or with a field that contains a ClientServiceThread), then another possibility is that Lot is a non-static inner class of a class that does have such a field. The outer class instance would then be a (hidden) member of Lot.
The solution is to either make ClientServiceThread serializable (not likely, from its name) or else eliminate it from the serialization by marking the relevant field(s) transient (or removing them from the Lot class).
Lot contains
public ArrayList<ClientServiceThread> clientBidsLog
private List<ClientServiceThread> clients
If you wish this field to be serialized mark the ClientServiceThread serializable too
if you don't want it to be serialized just mark it transient like
public transient ArrayList<ClientServiceThread> clientBidsLog
private transient List<ClientServiceThread> clients
A couple of answers have suggested that you could declare ClientServiceThread serializable as a possible solution.
WARNING - that probably won't work!
Yes, you can declare a Thread subclass that implements Serializable, but the Java serialization mechanism can't serialize the stack of a live thread. In fact, I don't even think it will succeed in serializing an inactive thread's state (e.g. the thread's ThreadGroup reference), so you'll probably end up the more exceptions.
I think your only option is to exclude the threads from serialization by declaring those collections to be transient.
I'm stumbled upon understanding java serialization. I have read in many documents and books that static and transient variables cannot be serialized in Java.
We declare a serialVersionUid as follows.
private static final long serialVersionUID = 1L;
If a static variable was not serialized then, we often face an exception during the de-serialization process.
java.io.InvalidClassException
in which the serialVersionUID from the deserialized object is extracted and compared with the serialVersionUID of the loaded class.
To my knowledge i think that if static variables cannot be serialized. There is no point of that exception. I may be wrong because I'm still learning.
Is it a myth that "Static and transient variables in java cannot be serialized". Please correct me, I'm in a mess about this concept.
Instance Variables: These variables are serialized, so during deserialization we will get back the serialized state.
Static Variables: These variables are not serialized, So during deserialization static variable value will loaded from the class.(Current value will be loaded.)
transient Variables: transient variables are not serialized, so during deserialization those variables will be initialized with corresponding default values (ex: for objects null, int 0).
Super class variables: If super class also implemented Serializable interface then those variables will be serialized, otherwise it won't serialize the super class variables. and while deserializing, JVM will run default constructor in super class and populates the default values. Same thing will happen for all superclasses.
serialVersionUID is a special static variable used by the serialization and deserialization process, to verify that a local class is compatible with the class used to serialize an object. It's not just a static variable as others, which are definitely not serialized.
When an object of a class is first serialized, a class descriptor containing among other things the class name and serial version UID is written to the stream. When this is deserialized, the JVM checks if the serial version UID read from the stream is the same as the one of the local class. If they're not, it doesn't even try to deserialize the object, because it knows the classes are incompatible.
serialVersionUID is special and is not subject to these rules. There is code within the serialization machinery that specifically handles this field to perform the automatic version checks.
The serialVersionUID is also serialized in this case.
Any static variable that is provided a value during class initialization is serialized.
However in normal cases, where you would provide the value to a static variable at the main class / run-time would not be serialized.
You can try to access the serialVersionUID by making it public and try to access it after deserialization.
You can refer "http://javabeginnerstutorial.com/core-java-tutorial/transient-vs-static-variable-java/" for more information.
Hope that helps. Cheers !!
You can test this for yourself - here's some example code that should answer your question:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class TestJava implements Serializable{
public static int k = 10;
public int j = 5;
public static void main(String[] args) {
TestJava tj1= new TestJava();
TestJava tj2;
try{ //serialization
FileOutputStream fos = new FileOutputStream("myclass.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(tj1);
oos.close();
fos.close();
System.out.println("object serielized 1..."+tj1.j);
System.out.println("object serielized 2..."+tj1.k);
System.out.println("object serielized 3..."+k);
k=++k; // 'k' value incrementd after serialization
} catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
} catch(IOException ioex){
ioex.printStackTrace();
}
try{ //deserialization
FileInputStream fis = new FileInputStream("myclass.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
tj2 = (TestJava) ois.readObject();
ois.close();
fis.close();
System.out.println("object DEEEEserielized 1..."+tj2.j);
System.out.println("object DEEEEserielized 2..."+tj2.k);
System.out.println("object DEEEEserielized 3..."+k);
// in deserialization 'k' value is shown as incremented.
// That means Static varialbe 'K' is not serialized.
// if 'K' value is serialized then, it has to show old value before incrementd the 'K' value.
} catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
} catch(IOException ioex){
ioex.printStackTrace();
} catch(ClassNotFoundException CNFE){
CNFE.printStackTrace();
}
}
}
This will output the following:
object serielized 1...5
object serielized 2...10
object serielized 3...10
object DEEEEserielized 1...5
object DEEEEserielized 2...11
object DEEEEserielized 3...11
So we create an object of class TestJava with one static integer field and one non-static field. We serialize the object, then - after serialization - increment the static integer.
When we later deserialize the object, we see that it has the incremented value, implying that it was not serialized.
Below example explains about static ,instance,transient and super class varialbes serialization and their outputs.
Serializing class:
public class SerializeEx extends SuperSerializeEx implements Serializable {
private static final long serialVersionUID = 1L;
public static int staticNumber = 1234;
public int instanceNumber = 1234;
public SerializeEx() {
staticNumber = 0;
instanceNumber = 0;
System.out.println("---sub class constructor---");
}
public SerializeEx(int staticNumber, int instanceNumber, int superNumber) {
super(superNumber);
this.staticNumber = staticNumber;
this.instanceNumber = instanceNumber;
}
}
Super Class:
public class SuperSerializeEx {
public int superNumber;
public SuperSerializeEx() {
System.out.println("---super class constructor---");
this.superNumber = 1000;
}
public SuperSerializeEx(int superNumber) {
this.superNumber = superNumber;
}
}
Serialization & Deserialization:
public class MainSerialization {
public static void main(String[] args) {
String fileName = "testing.txt";
serialize(fileName);
deSerialize(fileName);
}
public static void serialize(String fileName) {
System.err.println("Serialize.....");
SerializeEx serializeMe = new SerializeEx(10, 10, 10);
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(serializeMe);
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void deSerialize(String fileName) {
System.err.println("DeSerialize.....");
SerializeEx time = null;
FileInputStream fis = null;
ObjectInputStream in = null;
try {
fis = new FileInputStream(fileName);
in = new ObjectInputStream(fis);
time = (SerializeEx) in.readObject();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
System.err.println("Instance Numer = " + time.instanceNumber + " \tStatic Number= " + time.staticNumber + " \t Super Number= " + time.superNumber);
SerializeEx serializeMe = new SerializeEx(1001, 1001, 1001); //Modifying the static and instnce variables
System.err.println("Instance Numer = " + time.instanceNumber + " \tStatic Number= " + time.staticNumber + " \t Super Number= " + time.superNumber);
}
}
Output:
---super class constructor---
Serialize.....
DeSerialize.....
Instance Numer = 10 Static Number= 10 Super Number= 1000
Instance Numer = 10 Static Number= 1001 Super Number= 1000
No, if a class have static variable then at the time of serialization that variable will be skipped . because static variable is unique for all object and serialization is used for only save the object properties ( state of object ).
static variable is a property of class
Yes, static variable will be serialized if it is initialized at the time of declaration.
For example,
case 1 : without initialization at the time of declaration
class Person implements Serializable{
public String firstName;
static String lastName;
}
public class Employee {
public static void main(String[] args) {
Person p = new Person();
p.firstName="abc";
p.lastName="xyz";
//to do serialization
}
}
output :
//after deserialization
firstName= abc
lastName= null
case 2 : with initialization at the time of declaration
class Person implements Serializable{
public String firstName=="abc";
static String lastName="pqr";
}
public class Employee {
public static void main(String[] args) {
Person p = new Person();
p.firstName="abc";
p.lastName="xyz";
//to do serialization
}
}
output :
//after deserialization
firstName= abc
lastName= pqr
Any static variable which has been initialised at the time of declaration will be serialized.