
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Importance of SerialVersionUID keyword in Java?
SerialVersionUID
- The SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads the state of the object from a file.
- The SerialVersionUID can be used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible w.r.t serialization. If the deserialization object is different than serialization, then it can throw an InvalidClassException.
- If the serialVersionUID is not specified then the runtime will calculate a default serialVersionUID value for that class based on various aspects of the class.
Example
import java.io.*; class Employee implements Serializable { private static final long serialVersionUID = 5462223600l; int empId; String name; String location; Employee(int empId, String name, String location) { this.empId = empId; this.name = name; this.location = location; } void empData() { System.out.println("Employee Id is: "+ empId); System.out.println("Employee Name is: "+ name); System.out.println("Employee Location is: "+ location); } } public class EmployeeTest { public static void main(String[] args)throws Exception{ Employee emp = new Employee(115, "Raja", "Hyderabad"); emp.empData(); FileOutputStream fos = new FileOutputStream("E:\Employee.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(emp); System.out.println("Object Serialized"); } }
Output
Employee Id is: 115 Employee Name is: Raja Employee Location is: Hyderabad Object Serialized
- Related Articles
- Explain the importance of import keyword in java?
- Importance of StringReader class in Java?\n
- Importance of a JSONTokener in Java?\n
- Importance of join() method in Java?\n
- Importance of the parseBoolean() method in Java?\n
- Importance of the getCause() method in Java?\n
- What is the importance of Boolean class in Java?\n
- Importance of iterate() method of Stream API in Java 9?\n
- What is the importance of a SwingWorker class in Java?\n
- What is the importance of a WindowListener interface in Java?\n
- Use of explicit keyword in C++\n
- Importance of HashSet in Java
- New keyword in Java
- This keyword in Java
- super keyword in Java

Advertisements