Java.io.ObjectStreamClass.getFields() Method
Advertisements
Description
The java.io.ObjectStreamClass.getFields() method returns an array of the fields of this serializable class.
Declaration
Following is the declaration for java.io.ObjectStreamClass.getFields() method
public ObjectStreamField[] getFields()
Parameters
NA
Return Value
This method 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
Example
The following example shows the usage of java.io.ObjectStreamClass.getFields() 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 fields from ObjectStreamClass for integers
ObjectStreamField[] list = osc.getFields();
// print the fields
for (int i = 0; i < list.length; i++) {
System.out.println("" + list[i]);
}
// create a new object stream class for Calendar
ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);
// get the fields for Calendar class
ObjectStreamField[] list2 = osc2.getFields();
// print the fields
for (int i = 0; i < list2.length; i++) {
System.out.println("" + list2[i]);
}
}
}
Let us compile and run the above program, this will produce the following result:
I value Z areFieldsSet I firstDayOfWeek Z isTimeSet Z lenient I minimalDaysInFirstWeek I nextStamp I serialVersionOnStream J time [I fields [Z isSet Ljava/util/TimeZone; zone