
- 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 Class
Introduction
The Java.io.ObjectStreamClass class is Serialization's descriptor for classes. It contains the name and serialVersionUID of the class. The ObjectStreamClass for a specific class loaded in this Java VM can be found/created using the lookup method.
Class declaration
Following is the declaration for Java.io.ObjectStreamClass class −
public class ObjectStreamClass extends Object implements Serializable
Field
Following are the fields for Java.io.ObjectStreamClass class −
static ObjectStreamField[] NO_FIELDS − This is the serialPersistentFields value indicating no serializable fields.
Class methods
Sr.No. | Method & Description |
---|---|
1 |
Class<?> forClass()
This method returns the class in the local VM that this version is mapped to. |
2 |
ObjectStreamField getField(String name)
This method gets the field of this class by name. |
3 |
ObjectStreamField[] getFields()
This method returns an array of the fields of this serializable class. |
4 |
String getName()
This method returns the name of the class described by this descriptor. |
5 |
long getSerialVersionUID()
This method returns the serialVersionUID for this class. |
6 |
static ObjectStreamClass lookup(Class<?> cl)
This method finds the descriptor for a class that can be serialized. |
7 |
static ObjectStreamClass lookupAny(Class<?> cl)
This method returns the descriptor for any class, regardless of whether it implements Serializable. |
8 |
String toString()
This method returns a string describing this ObjectStreamClass. |
Methods inherited
This class inherits methods from the following classes −
- Java.io.Object
Example - Get the Class of a simple serializable object
The following example shows the usage of ObjectStreamClass forClass() method. We're using ObjectStreamClass.forClass() to inspect the class of a serialized object.
ObjectStreamClassDemo.java
package com.tutorialspoint; import java.io.ObjectStreamClass; import java.io.Serializable; public class ObjectStreamClassDemo { public static void main(String[] args) { ObjectStreamClass desc = ObjectStreamClass.lookup(Employee.class); if (desc != null) { System.out.println("Class from ObjectStreamClass: " + desc.forClass().getName()); } else { System.out.println("Class not serializable."); } } static class Employee implements Serializable { private static final long serialVersionUID = 1L; String name; int id; } }
Output
Let us compile and run the above program, this will produce the following result−
Class from ObjectStreamClass: com.tutorialspoint.ObjectStreamClassDemo$Employee
Explanation
ObjectStreamClass.lookup(Employee.class) returns metadata for the Employee class.
forClass() gives back the original Class object.
Helpful when reflecting over serialized types.
Example - Inspect a field in a simple serializable class
The following example shows the usage of ObjectStreamClass getField(String name) method. We're getting metadata for a field named "username" in a User class.
ObjectStreamClassDemo.java
package com.tutorialspoint; import java.io.ObjectStreamClass; import java.io.ObjectStreamField; import java.io.Serializable; public class ObjectStreamClassDemo { public static void main(String[] args) { ObjectStreamClass osc = ObjectStreamClass.lookup(User.class); ObjectStreamField field = osc.getField("username"); if (field != null) { System.out.println("Field name: " + field.getName()); System.out.println("Field type: " + field.getType().getSimpleName()); } else { System.out.println("Field 'username' not found."); } } static class User implements Serializable { private static final long serialVersionUID = 1L; String username; int age; } }
Output
Let us compile and run the above program, this will produce the following result−
Field name: username Field type: String
Explanation
ObjectStreamClass.lookup(User.class) gets the serialization metadata for User.
getField("username") returns the ObjectStreamField representing that field.
You can then access its name, type, and other properties.
Example - Print names of multiple serializable classes dynamically
The following example shows the usage of ObjectStreamClass getName() method. We're iterating through a list of classes and printing their names using ObjectStreamClass.getName().
ObjectStreamClassDemo.java
package com.tutorialspoint; import java.io.ObjectStreamClass; import java.io.Serializable; import java.util.List; public class ObjectStreamClassDemo { public static void main(String[] args) { List<Class<?>> classes = List.of(Order.class, String.class, Integer.class); for (Class<?> clazz : classes) { ObjectStreamClass osc = ObjectStreamClass.lookup(clazz); if (osc != null) { System.out.println("Serializable: " + osc.getName()); } else { System.out.println("Not serializable: " + clazz.getName()); } } } static class Order implements Serializable { private static final long serialVersionUID = 1L; String item; int quantity; } }
Output
Let us compile and run the above program, this will produce the following result−
Serializable: com.tutorialspoint.ObjectStreamClassDemo$Order Serializable: java.lang.String Serializable: java.lang.Integer
Explanation
getName() gives you the class names, whether built-in (String, Integer) or custom (Order).
This is useful for analyzing types at runtime in generic serialization utilities.