Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - ObjectStreamClass forClass() method



Description

The Java ObjectStreamClass forClass() method returns the class in the local VM that this version is mapped to. Null is returned if there is no corresponding local class.

  • It returns the Class<?> object associated with the ObjectStreamClass instance.

  • You typically obtain the ObjectStreamClass instance using ObjectStreamClass.lookup(Class<?>) during serialization.

Declaration

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

public Class<?> forClass()

Parameters

NA

Return Value

This method returns the Class instance that this descriptor represents.

Exception

NA

Example - Usage of ObjectStreamClass forClass() method

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

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.ObjectStreamClass;

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 Class instance for osc
      System.out.println("" + osc.forClass());

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

      // get the Class instance for osc
      System.out.println("" + osc2.forClass());
   }
}

Output

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

class java.lang.Integer
class java.lang.String

Example - Get the Class of a simple serializable object

The following example shows the usage of ObjectStreamClass forClass() method. We're using ObjectStreamClass.forClass() to inspect the class of a serialized object.

ObjectStreamClassDemo.java

package com.tutorialspoint;

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

public class ObjectStreamClassDemo {
   public static void main(String[] args) {
      ObjectStreamClass desc = ObjectStreamClass.lookup(Employee.class);
      if (desc != null) {
         System.out.println("Class from ObjectStreamClass: " + desc.forClass().getName());
      } else {
         System.out.println("Class not serializable.");
      }
   }

   static class Employee 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−

Class from ObjectStreamClass: com.tutorialspoint.ObjectStreamClassDemo$Employee

Explanation

  • ObjectStreamClass.lookup(Employee.class) returns metadata for the Employee class.

  • forClass() gives back the original Class object.

  • Helpful when reflecting over serialized types.

Example - Log class info before serialization

The following example shows the usage of ObjectStreamClass forClass() method. We're printing the class name and serial version UID before writing an object.

ObjectStreamClassDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;

public class ObjectStreamClassDemo {
   public static void main(String[] args) throws IOException {
      Product product = new Product("Mouse", 49.99);

      // Get ObjectStreamClass before writing
      ObjectStreamClass osc = ObjectStreamClass.lookup(Product.class);
      if (osc != null) {
         System.out.println("Serializing: " + osc.forClass().getSimpleName());
         System.out.println("serialVersionUID: " + osc.getSerialVersionUID());
      }

      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("product.ser"))) {
         oos.writeObject(product);
      }
   }

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

      public Product(String name, double price) {
         this.name = name;
         this.price = price;
      }
   }
}

Output

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

Serializing: Product
serialVersionUID: 123456

Explanation

  • forClass() returns Product.class at runtime.

  • Combined with getSerialVersionUID(), this is useful for serialization diagnostics, logging, or version checks.

java_io_objectstreamclass.htm
Advertisements