Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java IO ObjectStreamField
Next article icon

Java.io.ObjectStreamClass in Java

Last Updated : 06 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

This 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. It extends class Object and implement serialisable.

Fields: static ObjectStreamField[] NO_FIELDS– This is the serialPersistentFields value indicating no serializable fields.

Methods: 

Class forClass(): This method return the class in the local VM that this version is mapped to. Null is returned if there is no corresponding local class.

Syntax: public Class forClass()
Returns: the Class instance that this descriptor represents
Exception: NA

static ObjectStreamClass lookup(Class class): Find the descriptor for a class that can be serialized. Creates an ObjectStreamClass instance if one does not exist yet for class. Null is returned if the specified class does not implement java.io.Serializable or java.io.Externalizable.

Syntax: public static ObjectStreamClass lookup(Class cl)
Return: the class descriptor for the specified class
Exception: NA

static ObjectStreamClass lookupAny(Class class): Returns the descriptor for any class, regardless of whether it implements Serializable.

Syntax: public static ObjectStreamClass lookupAny(Class class)
Return: the class descriptor for the specified class
Exception: NA

Java




// Java code illustrating forClass(),
// lookup() and lookupAny() method
 
import java.io.ObjectStreamClass;
import java.util.ArrayList;
 
 
public class ObjectStreamDemo
{
    public static void main(String arg[])
    {
        // creating object stream class for Number
        ObjectStreamClass geeks_stream
            = ObjectStreamClass.lookup(Number.class);
        ObjectStreamClass quiz_stream
            = ObjectStreamClass.lookupAny(ArrayList.class);
         
        // checking class instance
        System.out.println(geeks_stream.forClass());
        System.out.println(quiz_stream.forClass());
         
    }
     
}
 
 

Output:

class java.lang.Number
class java.util.ArrayList

ObjectStreamField getField(String name): Get the field of this class by name.

Syntax: public ObjectStreamField getField(String name)
Return: The ObjectStreamField object of the named
field or null if there is no such named field.
Exception: NA

ObjectStreamField[] getFields(): Return an array of the fields of this serializable class.

Syntax: public ObjectStreamField[] getFields()
Returns: an array containing an element for each
persistent field of this class. Returns an array of length zero if
there are no fields.
Exception: NA

String getName(): 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.

Syntax: public String getName()
Return: a string representing the name of the class
Exception: NA

long getSerialVersionUID(): Return the serialVersionUID for this class. The serialVersionUID defines a set of classes all with the same name that have evolved from a common root class and agree to be serialized and deserialized using a common format. NonSerializable classes have a serialVersionUID of 0L.

Syntax: public long getSerialVersionUID()
Returns: the SUID of the class described by this descriptor
Exception: NA

String toString(): Return a string describing this ObjectStreamClass.

Syntax: public String toString()
Returns: a string representation of the object.
Exception: NA

Java




// Java code illustrating getField(), toString()
// getClass(), getSerialVersionUID()
 
import java.io.ObjectStreamClass;
import java.util.ArrayList;
 
 
public class ObjectStreamDemo
{
    public static void main(String arg[])
    {
        // creating object stream class for Number
        ObjectStreamClass geeks_stream
            = ObjectStreamClass.lookup(Number.class);
         
        // checking field
        System.out.println(geeks_stream.getField("quiz_stream"));
        System.out.println(geeks_stream.getFields());
         
        // class name
        System.out.println("class name: " + geeks_stream.getClass());
         
        // checking serial version UID
        System.out.println(geeks_stream.getSerialVersionUID());
         
        System.out.println(geeks_stream.toString());
    }
     
}
 
 

Output:

null
[Ljava.io.ObjectStreamField;@45ee12a7
class name: class java.io.ObjectStreamClass
-8742448824652078965
java.lang.Number: static final long serialVersionUID = -8742448824652078965L;



Next Article
Java IO ObjectStreamField
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Java
  • Java-I/O
Practice Tags :
  • Java

Similar Reads

  • Java.io.ObjectInputStream Class in Java | Set 1
    ObjectInputStream Class deserializes the primitive data and objects previously written by ObjectOutputStream. Both ObjectOutputStream and ObjectInputStream are used as it provides storage for graphs of object.It ensures that the object it is working for, matches the classes of JVM i.e Java Virtual M
    9 min read
  • Java.io.ObjectInputStream Class in Java | Set 2
    Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStrea
    6 min read
  • Java.io.SequenceInputStream in Java
    The SequenceInputStream class allows you to concatenate multiple InputStreams. It reads data of streams one by one. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of fil
    3 min read
  • Java.io.ObjectOutputStream Class in Java | Set 1
    An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. Only objects that support the java.io.Serializable in
    9 min read
  • Java IO ObjectStreamField
    The Java. io. ObjectStreamField class is a description of a Serializable field from a Serializable interface. This class is serialization’s descriptor for classes. An array of The ObjectStreamFields is utilized to maintain the Serializable fields of a class. It includes the name and serialVersionUID
    3 min read
  • Java.io.OutputStream class in Java
    This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo
    2 min read
  • Java.io Package in Java
    Java.io Package in JavaThis package provides for system input and output through data streams, serialization and the file system. Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown. Follo
    1 min read
  • Java.io.PipedInputStream class in Java
    Pipes in IO provides a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination. PipedInputStream is also piped with PipedOutputStream. So, data can be written using PipedOutputStream and can be written using PipedInputStream.But, using both threads
    5 min read
  • Java.io.PushbackInputStream class in Java
    Pushback is used on an input stream to allow a byte to be read and then returned (i.e, "pushed back") to the stream. The PushbackInputStream class implements this idea. It provides a mechanism "peek" at what is coming from an input stream without disrupting it. It extends FilterInputStream.Fields: p
    7 min read
  • Java.io.Printstream Class in Java | Set 2
    Java.io.Printstream Class in Java | Set 1More Methods: PrintStream printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this output stream using the specified format string and arguments. Syntax :public PrintStream printf(Locale l, String format, Obje
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences