ObjectStreamClass getField(String) in Java

getField(String): This method is available in the java.io.ObjectStreamClass class of Java.

Syntax:

ObjectStreamField java.io.ObjectStreamClass.getField(String name)

This method takes one argument. This method gets the field of this class by name.

Parameters: One parameter is required for this method.

name: the name of the data field to look for.

Returns: The ObjectStreamField object of the named field or null if there is no such named field.

Exceptions: NA

Approach

Java

import java.io.ObjectStreamClass;

public class ObjectStreamClassgetField {
    public static void main(String[] args) {

        ObjectStreamClass objectStreamClass =
ObjectStreamClass.lookup(String.class);

        System.out.println(objectStreamClass.getField("name"));
    }
}

Output:

null


Some more methods of ObjectStreamClass

forClass()This method returns the class to the local VM that this version is mapped to.

getFields()This method returns an array of the fields of this serializable class.

getName()This method returns the name of the class described by this descriptor.

getSerialVersionUID()This method returns the serialVersionUID for this class.

lookup(Class)This method creates an ObjectStreamClass instance if one does not exist yet for the class.

lookupAny(Class)This method returns the descriptor for any class, regardless of whether it implements Serializable.

toString()This method returns a string describing this ObjectStreamClass.

No comments:

Post a Comment