PUBLISHED ON: FEBRUARY 26, 2021
Java ObjectStreamField class
In this tutorial, we will learn about ObjectStreamField
class in Java. This class is a description of a Serializable field from a Serializable class. An array of ObjectStreamFields is used to declare the Serializable fields of a class.
Syntax
public class ObjectStreamField extends Object implements Comparable<Object>
Constructors
Constructor |
Description |
ObjectStreamField(String name, Class<?> type) |
This method creates a Serializable field with the specified type. |
ObjectStreamField(String name, Class<?> type, boolean unshared) |
This method creates an ObjectStreamField representing a serializable field with the given name and type. |
Methods
Methods of ObjectStreamField class are given below:
Method |
Description |
int compareTo(Object obj) |
It compares this field with another ObjectStreamField. |
String getName() |
It gets the name of this field. |
int GetOffset() |
The offset of a field within instance data. |
Class<?> getType() |
It gets the type of the field. |
char getTypeCode() |
It returns character encoding of the field type. |
String getTypeString() |
It returns the JVM type signature. |
boolean isPrimitive() |
It returns true if this field has a primitive type. |
boolean isUnshared() |
It returns a boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared. |
protected void setOffset(int offset) |
Offset within instance data. |
String toString() |
It returns a string that describes this field. |
The above table getTypeCode()
method returns char i.e. character encoding of the field type. The encoding is as follows:
B |
byte |
C |
char |
D |
double |
F |
float |
I |
int |
J |
long |
L |
class or interface |
S |
short |
Z |
boolean |
[ |
array |
Example of ObjectStreamField
In the following example, we are implementing getName()
and getField()
method of ObjectStreamField
class. This method returns a string representing the name of the serializable field.
package studytonight;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.util.Calendar;
public class StudyTonight
{
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("lenient");
System.out.println("" + field2.getName());
}
}
value
lenient
Example:
In this example, we are creating a new object stream for Integers and Calendar then getting the class instance for both and printing it.
package studytonight;
import java.io.ObjectStreamClass;
import java.util.Calendar;
public class StudyTonight
{
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"));
}
}
null
Z isTimeSet
Conclusion:
In this tutorial, we learned about ObjectStreamField
class in Java. This class is a description of a Serializable field from a Serializable class. An array of ObjectStreamFields is used to declare the Serializable fields of a class.