An explanation of a Serializable field within a Serializable class pertains to the declaration of Serializable fields in a class using an array of ObjectStreamFields.
The method getField(String name) from java.io.ObjectStreamClass retrieves the field of the current class based on its name.
Constructor
| Constructor | Description |
|---|---|
| ObjectStreamField(String name, Class<?> type) | It creates a Serializable field with the specified type. |
| ObjectStreamField(String name, Class<?> type) | It creates a Serializable field with the specified type. |
| ObjectStreamField(String name, Class<?> type, boolean unshared) | It creates an ObjectStreamField representing a serializable field with the given name and type. |
Methods
| Modifier and Type | Method | Description |
|---|---|---|
int |
compareTo(Object obj) | It compares this field with another ObjectStreamField. |
| String | getName() | It gets the name of this field. |
int |
GetOffset() | Offset of field within instance data. |
| Class<?> | getType() | It get the type of the field. |
char |
getTypeCode() | It returns character encoding of field type. |
| String | getTypeString() | It return theJVMtype signature. |
| boolean | isPrimitive() | It return true if this field has a primitive type. |
| boolean | isUnshared() | It returns 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 return astringthat describes this field. |
public char getTypeCode
Provide the character encoding for the field type, which is structured as follows:
| B | byte |
|---|---|
C |
char |
D |
double |
F |
float |
I |
int |
J |
long |
L |
class or interface |
S |
short |
Z |
boolean |
| [ | array |
Returns:
the typecode of the serializable field
Example:
import java.io.ObjectStreamClass;
import java.util.Calendar;
public class ObjectStreamClassExample {
public static void main(String[] args) {
// create a new object stream class for Integers
ObjectStreamClass osc = ObjectStreamClass.lookup(String.class);
// get the value field from ObjectStreamClass for integers
System.out.println("" + osc.getField("value"));
// create a new object stream class for Calendar
ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);
// get the Class instance for osc2
System.out.println("" + osc2.getField("isTimeSet"));
}
}
Output:
I value
Z isTimeSet