Java - ObjectStreamClass toString() method



Description

The Java ObjectStreamClass toString() method returns a string describing this ObjectStreamClass.

  • It returns a string representation of the ObjectStreamClass instance.

  • This includes −

    • The class name

    • The serialVersionUID

  • It's useful for logging, debugging, or simply inspecting what's being serialized.

Declaration

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

public static ObjectStreamClass toString()

Parameters

NA

Return Value

This method returns a string representation of the object.

Exception

NA

Example - Usage of ObjectStreamClass toString() method

The following example shows the usage of ObjectStreamClass toString() 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.lookupAny(Integer.class);

      // print the object as a string
      System.out.println("" + osc.toString());

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

      // print the object as a string
      System.out.println("" + osc2.toString());
   }
}

Output

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

java.lang.Integer: static final long serialVersionUID = 1360826667806852920L;
java.util.Calendar: static final long serialVersionUID = -1807547505821590642L;

Example - Print ObjectStreamClass info for a serializable class

The following example shows the usage of ObjectStreamClass toString() method. We're using using toString() to log metadata about the User class.

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(User.class);
        System.out.println("ObjectStreamClass info: " + osc.toString());
    }

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

Output

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

ObjectStreamClass info: com.tutorialspoint.ObjectStreamClassDemo$User: static final long serialVersionUID = 42L;

Explanation

  • toString() gives you a summary of the class name and serialVersionUID.

  • Useful for logging what's being serialized.

Example - Use toString() while processing multiple serializable classes

The following example shows the usage of ObjectStreamClass toString() method. We're logging metadata for multiple classes during batch inspection.

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(Employee.class, Product.class);

      for (Class<?> clazz : classes) {
         ObjectStreamClass osc = ObjectStreamClass.lookup(clazz);
         if (osc != null) {
            System.out.println("Class info: " + osc.toString());
         } else {
            System.out.println("Class " + clazz.getSimpleName() + " is not serializable.");
         }
      }
   }

   static class Employee implements Serializable {
      private static final long serialVersionUID = 1001L;
      String name;
   }

   static class Product implements Serializable {
      private static final long serialVersionUID = 2002L;
      String name;
      double price;
   }
}

Output

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

Class info: com.tutorialspoint.ObjectStreamClassDemo$Employee: static final long serialVersionUID = 1001L;
Class info: com.tutorialspoint.ObjectStreamClassDemo$Product: static final long serialVersionUID = 2002L;

Explanation

  • This example shows how you can use toString() to inspect multiple classes.

  • It helps track versioning info during development or debugging.

java_io_objectstreamclass.htm
Advertisements