Retrieve fields with data type Reflection Java

It is advised to read the notes on Java Reflection API before attempting the programs on this Retrieve fields with data type.

import java.lang.reflect.Field;
public class Demo extends Test
  public int price;
  public double rate;
  public String name;
  public static void main(String args[])
     Demo d1 = new Demo();
     Class c = d1.getClass();

     Field s[ ] = c.getDeclaredFields(); 
     for( int i = 0; i < s.length; i++)   
       System.out.println(s[i]);  // prints all fields
       c = s[i].getType() ;
       System.out.println("The above field is of type " + c.getName() + "\n") ;
     } // prints the type of each public  field

getDeclaredFields() returns all fields and getType() returns field's data type.

Along with this tutorial "Retrieve fields with data type", it is advised to read other programs on Reflection API to get over command.

Reflection API Programs

Leave a Comment

Your email address will not be published.