Java - ObjectStreamField getType() method



Description

The Java ObjectStreamField getType() method gets the type of the field. If the type is non-primitive and this ObjectStreamField was obtained from a deserialized ObjectStreamClass instance, then Object.class is returned. Otherwise, the Class object for the type of the field is returned.

  • getType() returns the Java Class type of the field represented by the ObjectStreamField.

  • It tells you whether the field is a String, int, double, boolean, or any other type.

  • It's useful for −

    • Field inspection

    • Building dynamic serializers

    • Debugging serialization structures

Declaration

Following is the declaration for java.io.ObjectStreamField.getType() method.

public Class<?> getType()

Parameters

NA

Return Value

This method returns a Class object representing the type of the serializable field.

Exception

NA

Example - Usage of ObjectStreamField getType() method

The following example shows the usage of ObjectStreamField getType() method.

ObjectStreamFieldDemo.java

package com.tutorialspoint;

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

public class ObjectStreamFieldDemo {
   public static void main(String[] args) {
   
      // create a new object stream class for Integers
      ObjectStreamClass osc = ObjectStreamClass.lookup(Integer.class);

      // get the field value from Integer class
      ObjectStreamField field = osc.getField("value");

      // get the type of the field
      System.out.println("" + field.getType());

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

      // get the field value from Calendar class
      ObjectStreamField field2 = osc2.getField("lenient");

      // get the type of the field2
      System.out.println("" + field2.getType());
   }
}

Output

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

int
boolean

Example - Print field names and their types

The following example shows the usage of ObjectStreamField getType() method. Inspect a Person class and print each field's name and type.

ObjectStreamFieldDemo.java

package com.tutorialspoint;

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

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

      System.out.println("Fields and their types 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;
      double height;
      transient String password; // will not appear (transient)
   }
}

Output

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

Fields and their types in Person:
 - name: String
 - age: int
 - height: double

Explanation

  • getType() shows the actual Java type of each serializable field.

  • password is transient, so it's excluded.

Example - Conditionally handle fields based on their type

The following example shows the usage of ObjectStreamField getType() method. We're printing a special message if the field type is String.

ObjectStreamFieldDemo.java

package com.tutorialspoint;

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

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

      for (ObjectStreamField field : fields) {
         if (field.getType() == String.class) {
            System.out.println("Field '" + field.getName() + "' is a String!");
         } else {
            System.out.println("Field '" + field.getName() + "' is of type: " + field.getType().getSimpleName());
         }
      }
   }

   static class Employee implements Serializable {
      private static final long serialVersionUID = 1L;
      String name;
      int id;
      boolean active;
   }
}

Output

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

Field 'active' is of type: boolean
Field 'id' is of type: int
Field 'name' is a String!

Explanation

  • getType() allows you to differentiate fields at runtime based on their types.

  • Very useful if you want to dynamically process only certain types (e.g., serialize only String fields).

java_io_objectstreamfield.htm
Advertisements