Java.io.ObjectStreamClass.getField() Method



Description

The java.io.ObjectStreamClass.getField(String name) method gets the field of this class by name.

Declaration

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

public ObjectStreamField getField(String name)

Parameters

name − The name of the data field to look for.

Return Value

This method returns the ObjectStreamField object of the named field or null if there is no such named field.

Exception

NA

Example

The following example shows the usage of java.io.ObjectStreamClass.getField() method.

package com.tutorialspoint;

import java.io.*;
import java.util.Calendar;

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 value field from ObjectStreamClass for integers
      System.out.println("" + osc.getField("value"));

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

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

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

I value
Z isTimeSet
java_io_objectstreamclass.htm
Advertisements