Retrieve fields of a class Reflection Java

It is advised to read the notes on Java Reflection API before attempting the programs.

Example on Retrieve fields of a class
import java.lang.reflect.Field;
public class Demo
  public int price;
  public double rate;
  public String name;
  public static void main(String args[])
     Demo d1 = new Demo();
     Class c = d1.getClass();

     Field f[] = c.getFields();
     for(int i = 0 ; i < f.length; i++)

getFields() of class java.lang.Class returns only public fields.

Leave a Comment

Your email address will not be published.