Java.io.ObjectStreamClass in Java

This 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. It extends class Object and implement serialisable.

Fields:
static ObjectStreamField[] NO_FIELDS– This is the serialPersistentFields value indicating no serializable fields.

Methods:

  1. Class forClass(): This method return the class in the local VM that this version is mapped to. Null is returned if there is no corresponding local class.

    Syntax: public Class forClass()
    Returns: the Class instance that this descriptor represents
    Exception: NA
    
  2. static ObjectStreamClass lookup(Class class): Find the descriptor for a class that can be serialized. Creates an ObjectStreamClass instance if one does not exist yet for class. Null is returned if the specified class does not implement java.io.Serializable or java.io.Externalizable.



    Syntax: public static ObjectStreamClass lookup(Class cl)
    Return: the class descriptor for the specified class
    Exception: NA
    
  3. static ObjectStreamClass lookupAny(Class class): Returns the descriptor for any class, regardless of whether it implements Serializable.

    Syntax: public static ObjectStreamClass lookupAny(Class class)
    Return: the class descriptor for the specified class
    Exception: NA
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating forClass(),
    // lookup() and lookupAny() method
      
    import java.io.ObjectStreamClass;
    import java.util.ArrayList;
      
      
    public class ObjectStreamDemo 
    {
        public static void main(String arg[])
        {
            // creating object stream class for Number
            ObjectStreamClass geeks_stream 
                = ObjectStreamClass.lookup(Number.class);
            ObjectStreamClass quiz_stream 
                = ObjectStreamClass.lookupAny(ArrayList.class);
              
            // checking class instance 
            System.out.println(geeks_stream.forClass());
            System.out.println(quiz_stream.forClass());
              
        }
          
    }

    chevron_right

    
    

    Output:

    class java.lang.Number
    class java.util.ArrayList
    
  4. ObjectStreamField getField(String name): Get the field of this class by name.

    Syntax: public ObjectStreamField getField(String name)
    Return: The ObjectStreamField object of the named 
    field or null if there is no such named field.
    Exception: NA
    
  5. ObjectStreamField[] getFields(): Return an array of the fields of this serializable class.

    Syntax: public ObjectStreamField[] getFields()
    Returns: an array containing an element for each 
    persistent field of this class. Returns an array of length zero if
     there are no fields.
    Exception: NA
    
  6. String getName(): Returns the name of the class described by this descriptor. This method returns the name of the class in the format that is used by the Class.getName() method.

    Syntax: public String getName()
    Return: a string representing the name of the class
    Exception: NA
    
  7. long getSerialVersionUID(): Return 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.

    Syntax: public long getSerialVersionUID()
    Returns: the SUID of the class described by this descriptor
    Exception: NA
    
  8. String toString(): Return a string describing this ObjectStreamClass.

    Syntax: public String toString()
    Returns: a string representation of the object.
    Exception: NA
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code illustrating getField(), toString()
    // getClass(), getSerialVersionUID()
      
    import java.io.ObjectStreamClass;
    import java.util.ArrayList;
      
      
    public class ObjectStreamDemo 
    {
        public static void main(String arg[])
        {
            // creating object stream class for Number
            ObjectStreamClass geeks_stream 
                = ObjectStreamClass.lookup(Number.class);
              
            // checking field
            System.out.println(geeks_stream.getField("quiz_stream"));
            System.out.println(geeks_stream.getFields());
              
            // class name 
            System.out.println("class name: " + geeks_stream.getClass());
              
            // chheking serial version UID
            System.out.println(geeks_stream.getSerialVersionUID());
              
            System.out.println(geeks_stream.toString());
        }
          
    }

    chevron_right

    
    

    Output:

    null
    [Ljava.io.ObjectStreamField;@45ee12a7
    class name: class java.io.ObjectStreamClass
    -8742448824652078965
    java.lang.Number: static final long serialVersionUID = -8742448824652078965L;
    

This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.