Java - ObjectStreamClass getName() method



Description

The Java ObjectStreamClass getName() method returns the name of the class described by this descriptor. This method returns the name of the class in the format that is used by the Class.getName() method.

  • getName() returns the fully qualified name of the class represented by the ObjectStreamClass instance.

  • You typically get the ObjectStreamClass using ObjectStreamClass.lookup(SomeClass.class).

  • It's useful for logging, serialization tools, and debugging.

Declaration

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

public String getName()

Parameters

NA

Return Value

This method returns a string representing the name of the class.

Exception

NA

Example - Usage of ObjectStreamClass getName() method

The following example shows the usage of ObjectStreamClass getName() 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 name of the class and print it
      System.out.println("" + osc.getName());

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

      // get the name of the class and print it
      System.out.println("" + osc2.getName());
   }
}

Output

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

java.lang.Integer
java.util.Calendar

Example - Get and print the name of a serializable class

The following example shows the usage of ObjectStreamClass getName() method. We're printing the name of the Customer class using ObjectStreamClass.getName().

ObjectStreamClassDemo.java

package com.tutorialspoint;

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

public class ObjectStreamClassDemo {
   public static void main(String[] args) {
      ObjectStreamClass osc = ObjectStreamClass.lookup(Customer.class);
      if (osc != null) {
         System.out.println("Serializable class name: " + osc.getName());
      } else {
         System.out.println("Class is not serializable.");
      }
   }

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

Output

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

Serializable class name: com.tutorialspoint.ObjectStreamClassDemo$Customer

Explanation

  • osc.getName() returns the fully qualified class name including the outer class if it's an inner class.

  • This is helpful for identifying which class is being serialized.

Example - Print names of multiple serializable classes dynamically

The following example shows the usage of ObjectStreamClass getName() method. We're iterating through a list of classes and printing their names using ObjectStreamClass.getName().

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.util.List;

public class ObjectStreamClassDemo {
   public static void main(String[] args) {
      List<Class<?>> classes = List.of(Order.class, String.class, Integer.class);

      for (Class<?> clazz : classes) {
         ObjectStreamClass osc = ObjectStreamClass.lookup(clazz);
         if (osc != null) {
            System.out.println("Serializable: " + osc.getName());
         } else {
            System.out.println("Not serializable: " + clazz.getName());
         }
      }
   }

   static class Order implements Serializable {
      private static final long serialVersionUID = 1L;
      String item;
      int quantity;
   }
}

Output

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

Serializable: com.tutorialspoint.ObjectStreamClassDemo$Order
Serializable: java.lang.String
Serializable: java.lang.Integer

Explanation

  • getName() gives you the class names, whether built-in (String, Integer) or custom (Order).

  • This is useful for analyzing types at runtime in generic serialization utilities.

java_io_objectstreamclass.htm
Advertisements