Java — SerialVersionUID in Java | Code Factory

Prerequisite : Serialization and Deserialization

private static final long SerialVersionUID=1234;
package com.example.java.programming;import java.io.Serializable;/**
* @author code.factory
*
*/
public class CodeFactory implements Serializable {
// User-defined SerialVersionUID
private static final long SerialVersionUID = 1234;
int i = 12;
int j = 34;
}
package com.example.java.programming;import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
/**
* @author code.factory
*
*/
public class Sender {
public static void main(String[] args) throws Exception {
CodeFactory cf = new CodeFactory();
// Here xyz.ser is the file name where the object is going to serialize
FileOutputStream fos = new FileOutputStream("xyz.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(cf);
}
}
package com.example.java.programming;import java.io.FileInputStream;
import java.io.ObjectInputStream;
/**
* @author code.factory
*
*/
public class Receiver {
public static void main(String... args) throws Exception {
// Here xyz.ser is the file name where the object is going to Deserialized
FileInputStream fis = new FileInputStream("xyz.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
CodeFactory cf = (CodeFactory) ois.readObject();
System.out.println("Deserialized Object Value : " + cf.i + "..." + cf.j);
}
}
Deserialized Object Value : 12...34

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store