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 –
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
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
Example for Method of Java IO ObjectStreamField ( compareTo(Object obj) ) –
The subsequent illustration explains the usefulness of the java.io.ObjectStreamField.compareTo() method –
Java
import java.io.*;
public class ObjectStreamField_ShowDemo {
public static void main(String[] args) {
ObjectStreamClass xyz = ObjectStreamClass.lookupAny(Integer. class );
ObjectStreamField field = xyz.getField( "value" );
ObjectStreamClass xyz2 = ObjectStreamClass.lookupAny(Float. class );
ObjectStreamField field2 = xyz.getField( "value" );
System.out.println( "" + field.compareTo(field2));
}
}
|
Output :
0
Share your thoughts in the comments
Please Login to comment...