Java ObjectStreamField class

Last Updated : 24 Jan 2026

The ObjectStreamField class in Java is used to describe a serializable field of a serializable class.

In this chapter, we will learn what the ObjectStreamField class is, why it is used, its constructors, important methods, type codes, and how it works with an example.

Constructors of ObjectStreamField Class

The ObjectStreamField class provides constructors to define a serializable field.

1. ObjectStreamField(String name, Class type)

This constructor creates an ObjectStreamField with the specified field name and data type.

Syntax:

Here is the syntax of this constructor:

2. ObjectStreamField(String name, Class type, boolean unshared)

This constructor creates an ObjectStreamField with the specified name and type and marks it as shared or unshared.

Syntax:

Here is the syntax of this constructor:

Methods of ObjectStreamField Class

The ObjectStreamField class provides methods to access serialization field details.

Modifier and TypeMethodDescription
intcompareTo(Object obj)It compares this field with another ObjectStreamField.
StringgetName()It gets the name of this field.
intGetOffset()Offset of field within instance data.
Class<?>getType()It get the type of the field.
chargetTypeCode()It returns character encoding of field type.
StringgetTypeString()It return the JVM type signature.
booleanisPrimitive()It return true if this field has a primitive type.
booleanisUnshared()It returns boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared.
protected voidsetOffset(int offset)Offset within instance data.
StringtoString()It return a string that describes this field.

Type Codes Returned by getTypeCode()

The getTypeCode() method returns a character that represents the field data type.

Bbyte
Cchar
Ddouble
Ffloat
Iint
Jlong
Lclass or interface
Sshort
Zboolean
[array

Returns:

the typecode of the serializable field

Example of ObjectStreamField Class

This example demonstrates how to retrieve serializable field details using the ObjectStreamField class.

Output:

I value
Z isTimeSet