Home » Java programming language

Java ObjectStreamField getType() Method with Example

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

ObjectStreamField Class getType() method

  • getType() method is available in java.io package.
  • getType() method is used to get the type of this ObjectStreamField field.
  • getType() 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.
  • getType() method does not throw an exception at the time of returning type.

Syntax:

    public Class getType();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is Class, it returns Class object for the type of the field is returned otherwise object Class is returned when the type is non-primitive and this ObjectStreamField was formed from a deserialized ObjectStreamClass field.

Example:

// Java program to demonstrate the example 
// of Class getType() method 
// of ObjectStreamField

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

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

  // By using getField() method is to get the field
  // value from Calendar 
  ObjectStreamField field1 = o_sc.getField("isTimeSet");
  ObjectStreamField field2 = o_sc.getField("fields");

  // By using getType() method is to return
  // the type of the field

  Class field1_type = field1.getType();
  System.out.println("field1.getType(): " + field1_type);

  Class field2_type = field2.getType();
  System.out.println("field2.getType(): " + field2_type);
 }
}

Output

field1.getType(): boolean
field2.getType(): class [I



Comments and Discussions!

Load comments ↻






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