Saturday 16 August 2014

Getting the class object

The entry point for all reflection operations is java.lang.Class. For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.Class which provides methods to examine the runtime properties of the object including its members and type information.

How to Retrieve Class Objects
There are 3 ways to retrieve a class Object.
   1. By using Object.getClass()
   2. By using .class syntax
   3. By using Class.forName

Using getClass()
Object class provides getClass() method, which returns the runtime class of this Object. This getClass() works for all reference types, since Object class is the super class for all classes in java.

public class Employee {
    String firstName, lastName;
    int id, age;
    
    String getFirstName(){
        return firstName;
    }
    
    String getLastName(){
        return lastName;
    }
    
    int getId(){
        return id;
    }
    
    int getAge(){
        return age;
    }
    
    void setFirstName(String firstName){
        this.firstName = firstName;
    }
    
    void setLastName(String lastName){
        this.lastName = lastName;
    }
    
    void setAge(int age){
        this.age = age;
    }
    
    void setId(int id){
        this.id = id;
    }
}

import java.lang.reflect.Method;

public class GetMethodNames {
    public static void main(String args[]){
        Employee emp = new Employee();
        
        Class myClass = emp.getClass();
        Method[] methNames = myClass.getDeclaredMethods();
        
        for(Method m : methNames){
            System.out.println(m.getName());
        }
        
        Object obj1 = new Object();
        obj1.getClass();
    }
}

Output
getId
getAge
setAge
setId
getFirstName
getLastName
setFirstName
setLastName

Using .class
You can get a class by appending ".class" to the name of the type.

import java.lang.reflect.Method;

public class GetMethodNames {
    public static void main(String args[]){        
        Class myClass = Employee.class;
        Method[] methNames = myClass.getDeclaredMethods();
        
        for(Method m : methNames){
            System.out.println(m.getName());
        }
        
        Object obj1 = new Object();
        obj1.getClass();
    }
}


Output
getId
getAge
setAge
setId
getFirstName
getLastName
setFirstName
setLastName

Using Class.forName()
If the fully-qualified name of a class is available (I.e, including the package names), it is possible to get the corresponding Class using the static method Class.forName().

import java.lang.reflect.Method;

public class GetMethodNames {
    public static void main(String args[]) throws ClassNotFoundException{        
        Class myClass = Class.forName("Employee");
        Method[] methNames = myClass.getDeclaredMethods();
        
        for(Method m : methNames){
            System.out.println(m.getName());
        }
        
        Object obj1 = new Object();
        obj1.getClass();
    }
}

Output
getId
getAge
setAge
setId
getFirstName
getLastName
setFirstName
setLastName






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment