Table of Contents
Java Serialization Tutorial:
- Serialization in java
- Java Serialization interview questions and answers
- serialversionuid in java serialization
- externalizable in java
- Transient keyword in java
- Difference between Serializable and Externalizable in Java
serialVersionUID is used to ensure that same class(That was used during Serialization) is loaded during Deserialization.serialVersionUID is used for version control of object.
If you have used Serialization then You might have seen serialVersionUID because whenever you implement Serializable interface your IDE will give you warning.
Serialversionuid Syntax:
As per java docs
1 2 3 |
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L; |
serialVersionUID must be Static and final.You can assign any number to it.
Lets see an example:
Create Employee.java in src->org.arpit.javapostsforlearning
1.Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.javapostsforlearning; import java.io.Serializable; public class Employee implements Serializable{ private static final long serialVersionUID = 1L; Â Â Â int employeeId; Â Â Â String employeeName; Â Â Â String department; Â Â Â Â Â Â public int getEmployeeId() { Â Â Â Â Â Â return employeeId; Â Â Â } Â Â Â public void setEmployeeId(int employeeId) { Â Â Â Â Â Â this.employeeId = employeeId; Â Â Â } Â Â Â public String getEmployeeName() { Â Â Â Â Â Â return employeeName; Â Â Â } Â Â Â public void setEmployeeName(String employeeName) { Â Â Â Â Â Â this.employeeName = employeeName; Â Â Â } Â Â Â public String getDepartment() { Â Â Â Â Â Â return department; Â Â Â } Â Â Â public void setDepartment(String department) { Â Â Â Â Â Â this.department = department; Â Â Â } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.javapostsforlearning; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; Â public class SerializeMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Employee emp = new Employee(); emp.setEmployeeId(101); emp.setEmployeeName("Arpit"); emp.setDepartment("CS"); try { FileOutputStream fileOut = new FileOutputStream("employee.ser"); ObjectOutputStream outStream = new ObjectOutputStream(fileOut); outStream.writeObject(emp); outStream.close(); fileOut.close(); }catch(IOException i) { i.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.arpit.javapostsforlearning; import java.io.IOException; import java.io.ObjectInputStream; public class DeserializeMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Employee emp = null; try { FileInputStream fileIn =new FileInputStream("employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); emp = (Employee) 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 Employee..."); System.out.println("Emp id: " + emp.getEmployeeId()); System.out.println("Name: " + emp.getEmployeeName()); System.out.println("Department: " + emp.getDepartment()); } } |
4.Run it:
1 2 3 4 5 6 |
Deserialized Employee... Emp id: 101 Name: Arpit Department: CS |
So when you run program,it was completed successfully and employee.ser has been created on disk.If you again run DeserializeMain.java,it will again run successfully. Now change value of variable serial to
1 2 3 |
private static final long serialVersionUID = 2L; |
and if you now run DeserializeMain.java it will give you following error.
1 2 3 |
java.io.InvalidClassException: org.arpit.javapostsforlearning.Employee; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2 |
So here during deserialization,we got error.It complained about Serialvesionuid being changed.But how does it know? because serialversionuid is a static variable and we know that “We can not serialize static variables”.How does it store serialversionuid? yes ,there is exception.Inspite of serialversionuid being static,it get serialized.So ObjectOutputStream writes every time to output stream and ObjectInputStream reads it back and if it does not have same values as in current version of class then it throw InvalidClassException.