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 -
|
Sr. No. | Constructor | Description |
|---|---|---|
| 1 | ObjectStreamField (String name, Class<?> type) | This Constructor Creates a Serializable field with the specified type to it |
| 2 | ObjectStreamField (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 |
|---|---|---|---|
| 1 | compareTo(Object obj) | int | This Method is Comparing this field with another ObjectStreamField |
| 2 | getName() | String | This method gives the name of the field |
| 3 | GetOffset() | int | This method returns the offset of the field within instance data |
| 4 | getType() | Class<?> | It gives the type of the field for the get type method |
| 5 | getTypeCode() | char | This method returns the character encoding of the field type |
| 6 | getTypeString() | String | This method returns the JVM type Signature |
| 7 | isPrimitive() | boolean | It returns true if the field has a primitive type |
| 8 | isUnshared() | boolean | It returns a boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared. |
| 9 | setOffset( int offset) | protected void | This method returns offset within the instance of the data |
| 10 | toString() | String | It returns a string that describes all this field |
Example:
// Java program to illustrate the working
// of the Java IO ObjectStreamField class
import java.io.ObjectStreamClass;
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 java.io.ObjectStreamField.compareTo() method -
// Java program to illustrate the working of the
// Java IO ObjectStreamField(compareTo(Object obj)) class
import java.io.*;
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 :
0