Tuesday, 14 November 2017

How to get the Constructors, methods, and variables of the class using Java Reflection


Click here to watch in Youtube :
https://www.youtube.com/watch?v=kNbCv03jDuA&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
How to get the Constructors, methods, and variables of the class using Java Reflection
Student.java
class Student
{
    public String name;
    public int age;
    private String gender;

    public Student()
    {

    }

    private Student(String name, int age)
    {
        super();
        this.name = name;
        this.age = age;
    }

    public void displayHello()
    {
        System.out.println("Hello");
    }
    
    
    private void displayWelcome()
    {
        System.out.println("Welcome");
    }

}
ReflectionDemo1.java
public class ReflectionDemo1
{
    public static void main(String[] args)
    {
        try
        {
            /*
             * forName method takes fully qualified name of classes or interface
             * as its argument and returns instance of the class assocaited with
             * it.
             */
            Class<?> classObj = Class.forName("Student");
            System.out.println(classObj.getName());
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }

    }

}
Output
Student

ReflectionDemo2.java
import java.lang.reflect.Constructor;

public class ReflectionDemo2
{
    public static void main(String[] args)
    {

        try
        {
            Class<?> classObj = Class.forName("Student");

            /*
             * getConstructors() method returns array of Constructors object
             * that represent all the public constructors of the invoking
             * object. Remember, this method only returns public constructors.
             */
            Constructor<?>[] ct = classObj.getConstructors();
            for (int i = 0; i < ct.length; i++)
            {
                System.out.println(ct[i]);
            }

            System.out.println("--------------------------------------");

            /*
             * If you want to see all the declared constructors of a class then
             * use getDeclaredConstructors()
             */
            Constructor<?>[] cdt = classObj.getDeclaredConstructors();
            for (int i = 0; i < cdt.length; i++)
            {
                System.out.println(cdt[i]);
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}
Output
public Student()
--------------------------------------
public Student()
private Student(java.lang.String,int)

ReflectionDemo3.java
import java.lang.reflect.Method;

public class ReflectionDemo3
{
    public static void main(String[] args)
    {
        try
        {
            Class<?> classObj = Class.forName("Student");

            /*
             * getMethods() method returns array of Method object that reflect
             * all the public method of invoking object.
             */
            Method[] methodArray = classObj.getMethods();
            for (int i = 0; i < methodArray.length; i++)
            {
                System.out.println(methodArray[i]);
            }

            System.out.println("---------------------------------");

            /*
             * getDeclaredMethods() returns only the declared methods of the
             * invoking class object.
             */
            Method[] declaredMethodArray = classObj.getDeclaredMethods();
            for (Method method : declaredMethodArray)
            {
                System.out.println(method);
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}
Output
public void Student.displayHello()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
---------------------------------
public void Student.displayHello()
private void Student.displayWelcome()

ReflectionDemo4.java
import java.lang.reflect.Field;

public class ReflectionDemo4
{
    public static void main(String[] args)
    {
        try
        {
            Class<?> classObj = Class.forName("Student");

            /*
             * getFields() returns an array containing Field objects reflecting
             * all the accessible public members of the class or interface
             * represented by this Class object.
             */
            Field[] fieldArray = classObj.getFields();
            for (Field field : fieldArray)
            {
                System.out.println(field);
            }

            System.out.println("-------------------------------");

            /*
             * getDeclaredFields() returns array of Field objects reflecting all
             * the fields declared by the class or interface represented by this
             * Class object.
             */
            Field[] declaredFieldArray = classObj.getDeclaredFields();
            for (Field field : declaredFieldArray)
            {
                System.out.println(field);
            }

        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}
Output
public java.lang.String Student.name
public int Student.age
-------------------------------
public java.lang.String Student.name
public int Student.age
private java.lang.String Student.gender

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_Intro_3_details.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_Intro_3_details

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/d5af812b33e9f57a10441740b1f80e2caf705d0c/BasicJava/ReflectionDemo_Intro_3_details/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment