×

Java Programs

Java Practice

Java program to get the list of fields of a class

Java example to get the list of fields of a class.
Submitted by Nidhi, on May 06, 2022

Problem statement

In this program, we will get the list of fields of a class using the getFields() method and print the result.

Source Code

The source code to get the list of fields of a class is given below. The given program is compiled and executed successfully.

// Java program to get the list of fields 
// of a class

import java.lang.reflect.Field;
public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    Class cls = Class.forName("java.lang.Double");

    Field fields[] = cls.getFields();

    System.out.println("Fields of Double class: ");
    for (Field field: fields)
      System.out.println(field);
  }
}

Output

Fields of Double class: 
public static final double java.lang.Double.POSITIVE_INFINITY
public static final double java.lang.Double.NEGATIVE_INFINITY
public static final double java.lang.Double.NaN
public static final double java.lang.Double.MAX_VALUE
public static final double java.lang.Double.MIN_NORMAL
public static final double java.lang.Double.MIN_VALUE
public static final int java.lang.Double.MAX_EXPONENT
public static final int java.lang.Double.MIN_EXPONENT
public static final int java.lang.Double.SIZE
public static final int java.lang.Double.BYTES
public static final java.lang.Class java.lang.Double.TYPE

Explanation

In the above program, we created a public class Main that contains a main() method. The main() method is the entry point for the program. In the main() method, we get the list of fields of the Double class using the getFields() method and printed the fields using the foreach loop.

Java Class and Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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