java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.lang.reflect arrow How to use Reflection in Java
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4095
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use Reflection in Java E-mail
User Rating: / 430
PoorBest 

Reflection is a powerful approach to analyze the class at runtime. If new classes are added into your application dynamically then Reflection is used to get the structure of the class.

Reflection uses special kind of java class: Class. The object of the Class type can hold all the information of the class and have getter methods to extract this information.

This example code extracts the structure of the String class. It will display the name of the constructors, declared fields and methods to the console.

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionExample {

  public static void main(String[] args) {
    try {
      // Creates an object of type Class which contains the information of 
      // the class String
      Class cl = Class.forName("java.lang.String");

      // getDeclaredFields() returns all the constructors of the class.
      Constructor cnst[] = cl.getConstructors();

      // getFields() returns all the declared fields of the class.
      Field fld[] = cl.getDeclaredFields();

      // getMethods() returns all the declared methods of the class.
      Method mtd[] = cl.getMethods();
      System.out.println("Name of the Constructors of the String class");

      for (int i = 0; i < cnst.length; i++) {
        System.out.println(cnst[i].getName());
      }

      System.out.println("Name of the Declared fields");

      for (int i = 0; i < fld.length; i++) {
        System.out.println(fld[i].getName());
      }

      System.out.println("Name of the Methods");

      for (int i = 0; i < mtd.length; i++) {
        System.out.println(mtd[i].getName());
      }

    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.