ObjectStreamClass getSerialVersionUID() in Java

getSerialVersionUID(): This method is available in the java.io.ObjectStreamClass class of Java.

Syntax:

long java.io.ObjectStreamClass.getSerialVersionUID()

This method returns the serialVersionUID for this class. The serialVersionUIDdefines a set of classes all with the same name that has evolved from a common root class and agrees to be serialized and deserialized using a common format.

NonSerializable classes have a serialVersionUID of 0L.

Parameters: NA

Returns: the SUID of the class described by this descriptor.

Exceptions: NA

Approach

Java

import java.io.ObjectStreamClass;

public class ObjectStreamClassgetSerialVersionUID {
    public static void main(String[] args) {

        ObjectStreamClass objectStreamClass =
ObjectStreamClass.lookup(String.class);

        System.out.println(objectStreamClass.getSerialVersionUID());
    }
}

Output:

-6849794470754667710


Some more methods of ObjectStreamClass

forClass()This method returns the class to the local VM that this version is mapped to.

getField(String)This method gets the field of this class by name.

getFields()This method returns an array of the fields of this serializable class.

getName()This method returns the name of the class described by this descriptor.

lookup(Class)This method creates an ObjectStreamClass instance if one does not exist yet for the class.

lookupAny(Class)This method returns the descriptor for any class, regardless of whether it implements Serializable.

toString()This method returns a string describing this ObjectStreamClass.

No comments:

Post a Comment