ADVERTISEMENT
ADVERTISEMENT

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 Solution:

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

Program/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 »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.