Java — SerialVersionUID in Java | Code Factory

Prerequisite : Serialization and Deserialization

  • Both sender and receiver should use the same JVM with respect to platform and version also. Otherwise receiver unable to deserialize because of different SerialVersionUID.
  • Both sender and receiver should use same .class file version. After serialization if there is any change in .class file at receiver side then receiver unable to deserialize.
  • To generate SerialVersionUID internally JVM may use complex algorithm which may create performance problem.
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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Stabilizing the Clouds: Multi-Cloud Architecture Load Balancing

Linked Missions and Obstacles in Hammer

NFTs & HellHounds Overview

I Wrote an Android Library Before the App.

How a Technical Project Manager Saves Your Money and Nerves

Top 5 DevOps Trends Business Enterprises Should Implement in 2022

Best Practices for Multi-Party Video Conferencing with the Vonage Video API

CS371p Spring 2022 Blog Wk8: Cristian Astorga

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
Code Factory

Code Factory

More from Medium

Java: Exception Handling

Why Spring Boot is called opinionated?

Java — Environment Setup

Spring vs Spring Boot