Sunday, October 11, 2009

Refletion API

Hope that everybody having problems with the reflection API will get some help from this tutorial.

This is what the sun java tutorials say about this API.

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.

you can get the full sun tutorial Here.

So sun says "This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language" so i don't know why they added this to CSE syllabus in the third lecture?????.

What to do now we must learn it so i thought to make a simple tutorial so everybody can understand.

Let's start

  • To look into java classes and find out what they are made of we must first be able to catch the class itself to do this there are three ways but we will only need one.
1.the simplest way
Class c=Class.forName("fully-qualified name of a class");

Here the fully qualified name means class with packages for example if we have class "foo" in package "packfoo"
the fully-qualified class name would be "";.

Then we have to find out the properties of the class like the name of it's super class,modifiers to do this you can use methods given in the Class class(look at all the methods Here).

Here are two methods from the Class class get the super class-c.getSuperclass() get the modifiers-c.getModifiers();
there a load of methods in the Class class which you can use similarly
  • Then we have to be able to get properties of methods in the class.
First we must get all the methods in the class to do this there are two methods in the Class class. get all the public methods and inherited methods-
Method m[ ]=c.getMethods();
Her "Method" is a variable type that can keep objects passed by the "getMethods();" and here we have used a array because this method return a array of "Method's". get all the other methods like private methods(includes public methodes but does not include inherited methods) -
Method m1[ ]=c.getDeclaredMethods();

Then just like the Class methods the 'Method' class also has a lot of useful methods like. m[0].getName(),m[0].getReturnType()

Now we have to get the variables for this it's same as the methods just two deferent methods to get the values.

1. to get all the public variables and inherited variables-
Field f[]=c.getFields();
Her "Field" is a variable type that can keep objects passed by the "getFields();" and here we have used a array because this method return a array of "Field's". get all the other variables like private variables(includes public variables but does not include inherited variables) -
Field f1[]=c.getDeclaredFields();

Then again 'Field' class also has a lot of useful methods like f[0].getName(),f[0].getModifiers();

Here is a simple example

The Main class

package javaapplication1;
public class Main {

public static void main(String[] args) {

Inspector insp = new Inspector("javaapplication1.NewClass");


The NewClass

package javaapplication1;

public class NewClass extends NewClass1 {
public int i=20;
public String name="supun";
int k;
private int y;
String nothing;
public int edan1(){
return 34;
int edan2(){
return 34;
private int edan3(){
return 34;

The NewClass1

package javaapplication1;

public class NewClass1 {
public int superr=21;

public String supermethod(){
return "super";
The inspector class (this does all the work)

package javaapplication1;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Inspector {
private Class thisClass;
private Class superClass;
public Inspector (String className) {
try {
System.out.println("Loading class : " + className);

thisClass = Class.forName(className);
System.out.println("Class load successful");

System.out.println("Prointing super class and modifier");
superClass = thisClass.getSuperclass();
System.out.println("Super class : "+ superClass);

Field[] f=thisClass.getFields();
Field[] f1=thisClass.getDeclaredFields();
System.out.println("get public feilds ");
for(Field x:f){
System.out.println("get other feilds ");
for(Field x:f1){

Method[] m1=thisClass.getDeclaredMethods();
Method[] m=thisClass.getMethods();
System.out.println("get public methods ");
for(Method x:m){
System.out.println("return type--"+x.getReturnType());
System.out.println("get other methods ");
for(Method x:m1){
System.out.println("return type--"+x.getReturnType());

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


Please feel free to comment on the tutorial

Friday, October 9, 2009

Getting Startd with JAVA -1

Now What's JAVA ??

java is one of the best and most used programming languages used these days.The things you can do with java are endless.And it is so vast that you cant just finish learing it.

So lets get started.lets first see what you will be needing to get started
1.first you need a computer(he he he) need to install some JDK version on your computer
The latest can be downloaded Here

3.A Software to Write programs in

It's realy easy to use one than to type on the notepad and run manualy.If you want to learn from scrap you should use JCreator LE.because it only provides the basic needs you can learn a lot you can get it Here.

If you want to do High level programing use NetBeans IDE You can find the latest version Here .This is not good for basic learning because it does more than half of the coding for you

4.Configuring your pc(this part is not needed if you use NetBeans)
You must set the eniviroment variable to the bin folder do as below

  • Find where the bin folder is an copy it's should be somting like this "C:\Program Files\Java\jdk1.6.0\bin"
  • then go to properties

Click on Enviromentel varables

And click on path go to the end of the existing variable value and put a ";" and paste tha path you

5.And the most important HOW TO CODE
Now to get Started with java obviously you need to get some good traning you can find some great tutorials Here. And if you would perfer books to learn theres a lot of books that are good
  1. sams teach yourself java in 21 days
  2. scjp by Kathy Sierra
  3. java how to program
  4. java 2 by Ivor Horton


Amazon Deals