Java ObjectStreamField Class

An explanation from a Serializable class regarding a Serializable field. A class’s Serializable fields are declared using an array of ObjectStreamFields.

Explanation

The Java Object Stream field class describes a serializable field from a serializable class. A class’s Serializable fields are declared using an array of ObjectStreamFields.

Constructor

Constructor  Description 
ObjectStreamField(String name, Class<?>  Using this method, a Serializable field of the given type is created.
ObjectStreamField(String name, Class<?> type, boolean unshared) This method uses the specified name and type to generate an ObjectStreamField representing a serializable field.

 

Type Method  Description 
compareTo(Object obj)                        Int Comparing this field with another ObjectStreamField is the method used here.
getName() String  This technique provides the field name.
Getoffset() Int This method returns the offset of the field within the instance data.
getType() Class It provides the field type for the get type method.
getTypeCode() Char This method returns the character encoding of the field type.
getTypeString() String  This method returns the JVM type signature.
isPrimitive() Boolean If the field has a primitive type, it returns true.
isUnshared() Boolean  If this ObjectStreamField instance represents a serializable field, it returns a boolean value indicating whether or not it is unshared.
setOffset( int offset)       Protected void This method gives back the offset within the data instance.
toString() String  A string describing every field in this field is returned.

The encoding are as follows:

B byte
D double 
F float
I int
J long
L class or interface
S short 
Z boolean
[ array 
C char

Example

import java.io.*; 
public class
Object { 

     

   public static void main(String[] args) {
      Object xyz = Object.lookupAny(Integer.class);

      Object field = xyz.getField("value");
      ObjectStreamClass xyz2 = ObjectStreamClass.lookupAny(Float.class); 

      Object field2 = xyz.getField("value");
      System.out.println("" + field.compareTo(field2)); 
        
   } 
}

Output
0

Example

package date;
import java.io.ObjectStreamClass;
import java.util.Calendar;
public class days
{
  public static void main(String args[])
  {
    ObjectStreamClass osc = ObjectStreamClass.lookup(String.class);  
    System.out.println("" + osc.getField("value"));  

    ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);  
    System.out.println("" + osc2.getField("isTimeSet"));  
  }
}

Output
Value
Z is Timeset

Example

package student;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.util.Calendar;
public class Std
{
  public static void main(String args[])
  {
    ObjectStreamClass osc = ObjectStreamClass.lookup(Integer.class);

    ObjectStreamField field = osc.getField("value");

    System.out.println("" + field.getName());

    ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);

    ObjectStreamField field2 = osc2.getField("Changed");

    System.out.println("" + field2.getName());
  }
}

Output
Value
Changed

Example

import java.io.ObjectStreamClass;   

import java.util.Calendar;   
public class ObjectStreamClassExample 
{   

     public static void main(String[] args) 

     {   
          ObjectStreamClass abc = ObjectStreamClass.lookup(String.class);   
          System.out.println("" + abc.getField("value"));   

          ObjectStreamClass abc2 = ObjectStreamClass.lookup(Calendar.class);   
          System.out.println("" + abc2.getField("isTimeSet"));   

       }   
}

Output
I value
Z isTimeSet

Conclusion

This class describes a Serializable field from a Serializable class. A class’s Serializable fields are declared using an array of ObjectStreamFields.