GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - ObjectStreamClass Class
Selected Reading
Java - ObjectStreamClass ClassIntroductionThe Java ObjectStreamClass class is Serialization's descriptor for classes. It contains the name and serialVersionUID of the class. The ObjectStreamClass for a specific class loaded in this Java VM can be found/created using the lookup method. Class declarationFollowing is the declaration for Java.io.ObjectStreamClass class − public class ObjectStreamClass
extends Object
implements Serializable
FieldFollowing are the fields for Java.io.ObjectStreamClass class −
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Get the Class of a simple serializable objectThe following example shows the usage of ObjectStreamClass forClass() method. We're using ObjectStreamClass.forClass() to inspect the class of a serialized object. ObjectStreamClassDemo.javapackage 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;
}
}
OutputLet us compile and run the above program, this will produce the following result− Class from ObjectStreamClass: com.tutorialspoint.ObjectStreamClassDemo$Employee Explanation
Example - Inspect a field in a simple serializable classThe 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.javapackage 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;
}
}
OutputLet us compile and run the above program, this will produce the following result− Field name: username Field type: String Explanation
Example - Print names of multiple serializable classes dynamicallyThe 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.javapackage 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;
}
}
OutputLet 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
Advertisements
| ||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |