
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectStreamClass getSerialVersionUID() method
Description
The Java ObjectStreamClass getSerialVersionUID() method returns the serialVersionUID for this class. The serialVersionUID defines a set of classes all with the same name that have evolved from a common root class and agree to be serialized and deserialized using a common format. NonSerializable classes have a serialVersionUID of 0L.
This method returns the serialVersionUID of a class.
serialVersionUID is a unique identifier used during serialization to verify class compatibility.
You call it on an ObjectStreamClass instance obtained via ObjectStreamClass.lookup(Class<?>).
Declaration
Following is the declaration for java.io.ObjectStreamClass.getSerialVersionUID() method.
public long getSerialVersionUID()
Parameters
NA
Return Value
This method returns the UUID of the class described by this descriptor.
Exception
NA
Example - Usage of ObjectStreamClass getSerialVersionUID() method
The following example shows the usage of ObjectStreamClass getSerialVersionUID() method.
ObjectStreamClassDemo.java
package com.tutorialspoint; import java.io.ObjectStreamClass; import java.util.Calendar; public class ObjectStreamClassDemo { public static void main(String[] args) { // create a new object stream class for Integers ObjectStreamClass osc = ObjectStreamClass.lookup(Integer.class); // get the serial for Integers System.out.println("" + osc.getSerialVersionUID()); // create a new object stream class for Calendar ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class); // get the serial for Calendar System.out.println("" + osc2.getSerialVersionUID()); } }
Output
Let us compile and run the above program, this will produce the following result −
1360826667806852920 -1807547505821590642
Example - Get serialVersionUID of a user-defined serializable class
The following example shows the usage of ObjectStreamClass getSerialVersionUID() method. We're printing the UID of a User class that explicitly defines serialVersionUID.
ObjectStreamClassDemo.java
package com.tutorialspoint; import java.io.ObjectStreamClass; import java.io.Serializable; public class ObjectStreamClassDemo { public static void main(String[] args) { ObjectStreamClass osc = ObjectStreamClass.lookup(User.class); System.out.println("serialVersionUID for User: " + osc.getSerialVersionUID()); } static class User implements Serializable { private static final long serialVersionUID = 123456789L; String name; int age; } }
Output
Let us compile and run the above program, this will produce the following result−
serialVersionUID for User: 123456789
Explanation
The serialVersionUID is explicitly declared in the class.
getSerialVersionUID() reflects that exact value.
This is commonly used to enforce version compatibility across Java versions or systems.
Example - Get default serialVersionUID (auto-generated)
The following example shows the usage of ObjectStreamClass getSerialVersionUID() method. We're inspecting the serialVersionUID of a class without a declared UID.
ObjectStreamClassDemo.java
package com.tutorialspoint; import java.io.ObjectStreamClass; import java.io.Serializable; public class ObjectStreamClassDemo { public static void main(String[] args) { ObjectStreamClass osc = ObjectStreamClass.lookup(Product.class); System.out.println("serialVersionUID for Product (auto-generated): " + osc.getSerialVersionUID()); } static class Product implements Serializable { String name; double price; } }
Output(Value will vary)
Let us compile and run the above program, this will produce the following result−
serialVersionUID for Product (auto-generated): -6562166864209567127
Explanation
Since Product does not define a serialVersionUID, the JVM auto-generates one based on the class structure.
That means if you modify the class (add/remove fields or methods), this value can change, breaking compatibility.