Let me clear you the use of serialVersionUID
while writing and reading objects to/from file.
In below code, I have written two functions writeObject()
and readObj()
writeObject()
is for writing the object in file
readObj()
is for reading the Object from file
package com.msq;
import java.io.Serializable;
public class A implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
int a;
transient int b;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}
package com.msq;
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;
public class B implements Serializable {
/**
*
*/
private static final long serialVersionUID = 123L ;
/**
*
*/
String name;
A a;
public B() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
public static void main(String[] args) {
//writeObject();
readObj();
}
static void writeObject() {
B b = new B();
b.setName("Musaddique");
A a2 = new A();
a2.setA(5);
a2.setB(10);
b.setA(a2);
ObjectOutputStream write = null;
try {
write = new ObjectOutputStream(new FileOutputStream(
"D:\\serObj.bat"));
write.writeObject(b);
write.flush();
write.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void readObj() {
ObjectInputStream reader = null;
try {
reader = new ObjectInputStream(
new FileInputStream("D:\\serObj.bat"));
B b1 = (B) reader.readObject();
System.out.println("name: "+b1.getName());
System.out.println("value of a: "+b1.getA().getA());
System.out.println("value of b: "+b1.getA().getB());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here I have used serialVersionUID = 123L
for class B and serialVersionUID = 1L
for class A and also used transient
keyword for variable b in order to restrict to save the value of b into file.
1) Write data to file then read the file you will get following output
name: Musaddique
value of a: 5
value of b: 0
you wil get value of b: 0 because we have used transient for b.
Now for testing try to write object by the same call but while reading change the serialVersionUID = 765L
then u will get below exception
java.io.InvalidClassException: com.msq.B; local class incompatible: stream classdesc serialVersionUID = 123, local class serialVersionUID = 765
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.msq.B.readObj(B.java:81)
at com.msq.B.main(B.java:46)
So, it is necessary to use same serialVersionUID
while reading and writing the object from file.
Also, it is used in RMI calls when you in-corporate classes from one machine to another machine or system.