Home » Java programming language

Java ObjectStreamClass getField() Method with Example

ObjectStreamClass Class getField() method: Here, we are going to learn about the getField() method of ObjectStreamClass Class with its syntax and example.
Submitted by Preeti Jain, on April 12, 2020

ObjectStreamClass Class getField() method

  • getField() method is available in java.io package.
  • getField() method is used to return the field of this ObjectStreamClass by the given field name (fi_na).
  • getField() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getField() method does not throw an exception at the time of returning named field.

Syntax:

    public ObjectStreamField getField(String fi_na);

Parameter(s):

  • String fi_na – represents the name of the returned field.

Return value:

The return type of the method is ObjectStreamField, it returns the ObjectStreamField object of the named field when exists otherwise it returns null when no named field exists.

Example:

// Java program to demonstrate the example 
// of ObjectStreamField getField(String fi_na)
// method of ObjectStreamClass

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

public class GetField {
 public static void main(String[] args) {
  // Instantiates ObjectStreamClass for
  // and Calendar
  ObjectStreamClass o_stm = ObjectStreamClass.lookup(Calendar.class);

  // By using getField() method is to return
  // the field of this class by name
  ObjectStreamField o_sf1 = o_stm.getField("isTimeSet");
  ObjectStreamField o_sf2 = o_stm.getField("fields");

  // Display o_sf1, o_sf2
  System.out.println("o_stm.getField(isTimeSet): " + o_sf1);
  System.out.println("o_stm.getField(fields): " + o_sf2);
 }
}

Output

o_stm.getField(isTimeSet): Z isTimeSet
o_stm.getField(fields): [I fields



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.