Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java IO ObjectStreamField

  • Last Updated : 26 Oct, 2021

The Java. io. ObjectStreamField class is a description of a Serializable field from a Serializable interface. This class is serialization’s descriptor for classes. An array of The ObjectStreamFields is utilized to maintain the Serializable fields of a class. It includes the name and serialVersionUID of the class.

The Class Declaration for Java IO ObjectStreamField is as –

1. public class ObjectStreamField extends Object       // extends Keyword is used  

2. public class ObjectStreamField implements Comparable<Object>   // implements Keyword is used

Constructors of Java IO ObjectStreamField 

Following Are The Two Constructors For The Class  Java IO ObjectStreamField –



1ObjectStreamField (String name, Class<?> type)This Constructor Creates a Serializable field with the specified type to it                                                                
2ObjectStreamField (String name, Class<?> type, boolean unshared)                                                                                                      This Constructor Creates an ObjectStreamField representing a serializable field with the given name and type of it

Methods of Java IO ObjectStreamField 

S.No.     Method Method  Type                                                    Description      
1compareTo(Object obj)                                     int This Method is Comparing this field with another ObjectStreamField
2getName() String This method gives the name of the field
3GetOffset() int This method returns the offset of the field within instance data
4getType() Class<?>It gives the type of the field for the get type method
5getTypeCode() char This method returns the character encoding of the field type
6getTypeString() String This method returns the JVM type Signature
7isPrimitive() boolean It returns true if the field has a primitive type
8isUnshared() boolean It returns a boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared.
9setOffset( int offset)                           protected void This method returns offset within the instance of the data
10toString()  String It returns a string that describes all this field



// Java program to illustrate the working
// of the Java IO ObjectStreamField class
import java.util.Calendar;  
public class ObjectStreamClassExample
     public static void main(String[] args)
          // creating a new object for the stream class for the Integers  
          ObjectStreamClass abc = ObjectStreamClass.lookup(String.class);  
          // getting the value field from ObjectStreamClass for the integers  
          System.out.println("" + abc.getField("value"));  
          // creating new object stream class for the  Calendar  
          ObjectStreamClass abc2 = ObjectStreamClass.lookup(Calendar.class);  
          // get the Class instance for abc2  
          System.out.println("" + abc2.getField("isTimeSet"));  

Output –

I value
Z isTimeSet

Example for Method of Java IO ObjectStreamField ( compareTo(Object obj) ) –   

The subsequent illustration explains the usefulness of the method –


// Java program to illustrate the working of the
// Java IO ObjectStreamField(compareTo(Object obj)) class
public class ObjectStreamField_ShowDemo {
   public static void main(String[] args) {
      // creating a new object stream class for The Integers
      ObjectStreamClass xyz = ObjectStreamClass.lookupAny(Integer.class);
      // getting the field value from Integer class
      ObjectStreamField field = xyz.getField("value");
      // creating a new object stream class for floats
      ObjectStreamClass xyz2 = ObjectStreamClass.lookupAny(Float.class);
      // getting the field value from Integer class
      ObjectStreamField field2 = xyz.getField("value");
      // comparing with another field
      System.out.println("" + field.compareTo(field2));

Output :


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!