Java Reflection example

Reflection is capability of software to analyse itself. java.lang.reflect package has interfaces and classes to get any detail of an object or class, during runtime. Below is java program, showing java reflection.

Below is English meaning of word, Reflection
the throwing back by a surface of sound, light, heat, etc.

package com.java652;
import java.lang.reflect.*;

class Simple{
public void xyz()
{

}

public int met()
{
    return 0;
}
}
interface My{}

class Javalang{
 public static void main(String args[]){
  try{
   Class c=Class.forName("com.java652.Simple");
   System.out.println("is Simple an interface?"+c.isInterface());

   Method mets[] = c.getMethods();
   for(Method met:mets)
   {
       System.out.println(met.getName());
   }

   Class c2=Class.forName("com.java652.My");
   System.out.println("is My an interface?"+c2.isInterface());

  }catch(Exception e){System.out.println(e);}

 }
}

Download Java Reflection Example, Eclipse Project

You may also like to read:
Dynamically invoke method using reflection

One thought on “Java Reflection example”

Leave a Reply