Java Reflection Mechanism
May 27th, 2009 | By admin | Category: JavaJava reflection API provides a way to know more about your own class which includes methods and fields. The power of reflection helps you load a class dynamically , create an object of a class at the run time , inspect your fields , methods and modifiers at the run time and many more.There are few draw-backs of the Java Reflection and we are going to discuss that at the end of this tutorial. Lets start with a very simple way to know about your class , super class and modifiers.
Listing 1 : Know about class , super class and modifiers
private static void getBasicClassInfo() {
// User is a class in package com.etechguide.java.reflection
User user = new User();
Class klass = user.getClass();
// Another way of getting class object
// Class klass = User.class;
Class superKlass = klass.getSuperclass();
int modifier = klass.getModifiers();
System.out.println("Class is :" + klass);
System.out.println("Super Class of User Class is :" + superKlass);
System.out.println("Is Public Class : " + Modifier.isPublic(modifier));
System.out.println("Is final Class : " + Modifier.isFinal(modifier));
System.out.println("Is Abstract Class : "
+ Modifier.isAbstract(modifier));
}
Have a look at the code in Listing 1 . At the line number 3 we create an object of a class User which is under a package say , com.etechguide.java.reflection. At the line number 4 we create a java.lang.Class object(klass) from the user object.This class object(klass) gives us the handle to get all the information of the User class.At the line number 7 we get the super class of the class User and we use the java.lang.reflect.Modifier from reflection API at the line number 11 , 12 and 13 to know if the class is final public , final or abstract.Modifier class uses the integer flag to determine the type modifier of the class.
output :
Class is :class com.etechguide.java.reflection.User
Super Class of User Class is :class java.lang.Object
Is Public Class : true
Is final Class : false
Is Abstract Class : false
java.lang.Class object can be archived in two other ways.Lets have a look at the following code below ,
Listing 2 : Know about Class.forname
private static Object instantiateUserWithDefaultConstructor()
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
Class klass = Class.forName("com.etechguide.java.reflection.User");
Object userObj = klass.newInstance();
// This will call the toString method of User Object
System.out.println("User object created : \n" + userObj);
return userObj;
}
At the line number 5 , we create a java.lang.Class object by using Class.forname(class_name) method. forname(class_name) is a static method of the class java.lang.Class.Point to remember is forname(class-name) method takes a String class name as argument and create the java.lang.Class object. The class name passed as an argument to the method , should be a fully qualified class name ( with full package name) not a class name alone.
At the line number 6 , we create an instance of the class User. The java.lang.Class object for the class com.etechguide.java.reflection.User is the klass and we create an instance of com.etechguide.java.reflection.User by calling klass.newInstance().
output :
The output of the program should be the toString method called on the User object.
It’s time to go deep into reflection and get more details on the classes and objects. In my last two example codes in Listing 1 and Listing 2 , I have used a class called com.etechguide.java.reflection.User. Lets have a full picture of the com.etechguide.java.reflection.User class .
Listing 3 : User class
package com.etechguide.java.reflection;
import java.math.BigDecimal;
public class User {
private String userId;
private String userName;
private String userAddress;
private String phoneNumber;
private BigDecimal income;
private boolean isMarried;
public User() {
super();
System.out.println("Creating object with default constructor ... ");
userId = "admin";
userName = "Administrator";
userAddress = "Geneva";
phoneNumber = "001384959569";
income = new BigDecimal(356786);
isMarried = true;
}
public User(String userId, String userName, String userAddress,
String phoneNumber, BigDecimal income, boolean isMarried) {
System.out
.println("Creating object with parameterized constructor ... ");
this.userId = userId;
this.userName = userName;
this.userAddress = userAddress;
this.phoneNumber = phoneNumber;
this.income = income;
this.isMarried = isMarried;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public BigDecimal getIncome() {
return income;
}
public void setIncome(BigDecimal income) {
this.income = income;
}
public boolean isMarried() {
return isMarried;
}
public void setMarried(boolean isMarried) {
this.isMarried = isMarried;
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("userId " + userId + "\n");
buffer.append("userName " + userName + "\n");
buffer.append("userAddress " + userAddress + "\n");
buffer.append("phoneNumber " + phoneNumber + "\n");
if (income != null)
buffer.append("income " + income.intValue() + "\n");
buffer.append("isMarried " + isMarried);
return buffer.toString();
}
private void SOP(String message) {
System.out.println("SOP Message");
System.out.println(message);
}
}
Our next target is to use java reflection to instantiate the User class by using the parameterized constructor of User class. Have a look at the following code in the next page .

Hello,
maybe you like this project. It’s a DSL for dealing with reflection.
http://projetos.vidageek.net/mirror
Thanks jonas for your project. that’s cool and neat.
- admin
Please do share your valuable learnings like this .Lets make this blog more useful.
http://etechguide.in
any updates coming ?
Hi. I like the way you write. Will you post some more articles?
any updates coming ?
emm. really like it.
Hi KonstantinMiller,
I have posted few more articles on different subjects.
-Admin