Java - ObjectStreamClass getField(String name) method



Description

The Java ObjectStreamClass getField(String name) method gets the field of this class by name.

  • This method returns a ObjectStreamField representing a named field in a serializable class.

  • It is useful for inspecting the fields that will be serialized.

  • You typically use this for debugging, custom serialization logic, or dynamic field analysis.

Declaration

Following is the declaration for java.io.ObjectStreamClass.getField(String name) method.

public ObjectStreamField getField(String name)

Parameters

name − The name of the data field to look for.

Return Value

This method returns the ObjectStreamField object of the named field or null if there is no such named field.

Exception

NA

Example - Usage of ObjectStreamClass getField(String name) method

The following example shows the usage of ObjectStreamClass getField(String name) method.

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
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 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

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

I value
Z isTimeSet

Example - Inspect a field in a simple serializable class

The following example shows the usage of ObjectStreamClass getField(String name) method. We're getting metadata for a field named "username" in a User 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(User.class);
      ObjectStreamField field = osc.getField("username");

      if (field != null) {
         System.out.println("Field name: " + field.getName());
         System.out.println("Field type: " + field.getType().getSimpleName());
      } else {
         System.out.println("Field 'username' not found.");
      }
   }

   static class User implements Serializable {
      private static final long serialVersionUID = 1L;
      String username;
      int age;
   }
}

Output

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

Field name: username
Field type: String

Explanation

  • ObjectStreamClass.lookup(User.class) gets the serialization metadata for User.

  • getField("username") returns the ObjectStreamField representing that field.

  • You can then access its name, type, and other properties.

Example - Try to get a non-existent field and handle it

The following example shows the usage of ObjectStreamClass getField(String name) method. We're trying to access a field named "email" that doesn't exist.

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(Profile.class);
      ObjectStreamField field = osc.getField("email");

      if (field == null) {
         System.out.println("Field 'email' not found in class: " + osc.getName());
      } else {
         System.out.println("Found: " + field.getName());
      }
   }

   static class Profile implements Serializable {
      private static final long serialVersionUID = 1L;
      String name;
      int age;
   }
}

Output

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

Field 'email' not found in class: com.tutorialspoint.ObjectStreamClassDemo$Profile

Explanation

  • Since "email" doesn't exist in Profile, getField() returns null.

  • It's a safe way to check for the presence of specific fields before acting on them.

java_io_objectstreamclass.htm
Advertisements