Advance Java

Learn New things from Rixosys

Finding out the class fields

Finding out the class fields

This section explores you, how to retrieve the fields of the class by using the getFields() method. For this we are taking an example that provides the use of the getFields() method in detailed way.

Create a class "Ffield" and create an object of this class and assign it the reference of java.util.Integer.class class. Now retrieve the class fields by using the getFields() method.

Here is the code of the Example :

import java.lang.reflect.*;
public class Ffield{
  public static void main(String[] args){
  Class cls = java.lang.String.class;
  Field field = cls.getFields()[0];
  String name;
  name = field.getName(); // It'll show CASE_INSENSITIVE_ORDER

Here is the output of the Example :


C:\rixsosys>java Ffield