Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java ObjectStreamClass

In this tutorial, we will learn about ObjectStreamClass in Java. This class is also known as serialization's descriptor for classes. It contains the name and serialVersionUID of the class.

Methods of ObjectStreamClass

Different methods of ObjectStreamClass are given below:

Method Description
Class<?> forClass() It returns the class in the local VM that this version is mapped to.
ObjectStreamField getField(String name) It gets the field of this class by name.
ObjectStreamField[] getFields() It returns an array of the fields of this serialization class.
String getName() It returns the name of the class described by this descriptor.
long getSerialVersionUID() It returns the serialVersionUID for this class.
Static ObjectStreamClass lookup(Class<?> cl) It finds the descriptor for a class that can be serialized.
Static ObjectStreamClass lookupAny(Class<?> cl) It returns the descriptor for any class, regardless of whether it implements Serializable.
String toString() It returns a string describing this ObjectStreamClass.

Example of ObjectStreamClass and its Methods

In the following example, we are calling getFields(), getName(), getSerialVersionUID() and toString() methods to get that corresponding information of class. Here we created an object of ObjectStreamClass method with the help of lookup() method and for Number class of java.lang package.

package studytonight;
import java.io.ObjectStreamClass;
public class StudyTonight 
{
	public static void main(String args[])
	{
		ObjectStreamClass stream = ObjectStreamClass.lookup(Number.class); 

		System.out.println("Class Fields: "+stream.getFields()); 
  
		System.out.println("Class Name: " + stream.getName()); 

		System.out.println("Class SerialVersionUID: "+stream.getSerialVersionUID()); 

		System.out.println("Class String: "+stream.toString()); 
	}
}


Class Fields: [Ljava.io.ObjectStreamField;@3ac42916
Class Name: java.lang.Number
Class SerialVersionUID: -8742448824652078965
Class String: java.lang.Number: static final long serialVersionUID = -8742448824652078965L;

Example of checking for instance of the class

In this example, we are checking the instance of a class using lookup() and lookupAny() method. Here lookup() methods find the descriptor for a class that can be serialized. On the other hand, lookupAny() method returns the descriptor for any class, regardless of whether it implements Serializable.

package studytonight;
import java.io.ObjectStreamClass;
import java.util.ArrayList;
public class StudyTonight 
{
	public static void main(String args[])
	{
		ObjectStreamClass objectStream1 = ObjectStreamClass.lookup(Number.class);
		System.out.println(objectStream1.forClass()); 
		ObjectStreamClass objectStream2 = ObjectStreamClass.lookupAny(ArrayList.class);         
		System.out.println(objectStream2.forClass()); 
	}
}


class java.lang.Number
class java.util.ArrayList

Conclusion

In this tutorial, we learned about ObjectStreamClass. This class is also known as serialization's descriptor for classes. It contains the name and serialVersionUID of the class. It extends class Objects and implements serializable.