Java - ObjectStreamClass getFields() method



Description

The Java ObjectStreamClass getFields() method returns an array of the fields of this serializable class.

  • getFields() returns an array of ObjectStreamField objects.

  • Each ObjectStreamField describes a field in a serializable class that is included in the serialization stream.

  • Useful for inspecting which fields Java will actually serialize.

Declaration

Following is the declaration for java.io.ObjectStreamClass.getFields() method.

public ObjectStreamField[] getFields()

Parameters

NA

Return Value

This method returns an array containing an element for each persistent field of this class. Returns an array of length zero if there are no fields.

Exception

NA

Example - Usage of ObjectStreamClass getFields() method

The following example shows the usage of ObjectStreamClass getFields() method.

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.util.Calendar;

public class ObjectStreamClassDemo {
   public static void main(String[] args) {

      // create a new object stream class for Integers
      ObjectStreamClass osc = ObjectStreamClass.lookup(Integer.class);

      // get the fields from ObjectStreamClass for integers
      ObjectStreamField[] list = osc.getFields();

      // print the fields
      for (int i = 0; i < list.length; i++) {
         System.out.println("" + list[i]);
      }

      // create a new object stream class for Calendar
      ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);

      // get the fields for Calendar class
      ObjectStreamField[] list2 = osc2.getFields();

      // print the fields
      for (int i = 0; i < list2.length; i++) {
         System.out.println("" + list2[i]);
      }

   }
}

Output

Let us compile and run the above program, this will produce the following result −

I value
Z areFieldsSet
I firstDayOfWeek
Z isTimeSet
Z lenient
I minimalDaysInFirstWeek
I nextStamp
I serialVersionOnStream
J time
[I fields
[Z isSet
Ljava/util/TimeZone; zone

Example - List all serializable fields in a class

The following example shows the usage of ObjectStreamClass getFields() method. We're printing the name and type of each serializable field in a Person class.

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;

public class ObjectStreamClassDemo {
   public static void main(String[] args) {
      ObjectStreamClass osc = ObjectStreamClass.lookup(Person.class);
      ObjectStreamField[] fields = osc.getFields();

      System.out.println("Serializable fields in Person:");
      for (ObjectStreamField field : fields) {
         System.out.println(" - " + field.getName() + " : " + field.getType().getSimpleName());
      }
   }

   static class Person implements Serializable {
      private static final long serialVersionUID = 1L;
      String name;
      int age;
      transient String password; // not serialized
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Serializable fields in Person:
 - name : String
 - age : int

Explanation

  • getFields() returns only the fields that will be serialized.

  • The transient field password is excluded.

  • You can use this for logging, custom tools, or dynamic serialization logic.

Example - Use reflection to compare declared fields vs. serializable fields

The following example shows the usage of ObjectStreamClass getFields() method. We're showcasing the difference between declared fields and the fields returned by getFields()

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.lang.reflect.Field;

public class ObjectStreamClassDemo {
   public static void main(String[] args) {
      Class<?> clazz = Account.class;
      ObjectStreamClass osc = ObjectStreamClass.lookup(clazz);

      System.out.println("Declared fields:");
      for (Field field : clazz.getDeclaredFields()) {
         System.out.println(" - " + field.getName());
      }

      System.out.println("\nSerializable fields:");
      for (ObjectStreamField f : osc.getFields()) {
         System.out.println(" - " + f.getName());
      }
   }

   static class Account implements Serializable {
      private static final long serialVersionUID = 1L;

      String owner;
      long balance;
      transient boolean loggedIn; // transient field
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Declared fields:
 - owner
 - balance
 - loggedIn

Serializable fields:
 - owner
 - balance

Explanation

  • getDeclaredFields() from reflection shows all fields.

  • getFields() from ObjectStreamClass shows only those included in serialization (i.e., non-transient and non-static).

java_io_objectstreamclass.htm
Advertisements