Tuesday, 24 October 2017

How to get the object of Class class | Java Reflection | Reflection in java


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

Click the below Image to Enlarge
How to get the object of Class class | Java Reflection | Reflection in java
Student.java
package com;

public class Student
{
    private String name;
    private int age;

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

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

}
ReflectionDemo1.java
public class ReflectionDemo1
{
    public static void main(String[] args)
    {
        try
        {
            /*
             * forName() method is used to load the class dynamically
             *
             * Returns:the Class object for the class with the
             * specified name.
             *
             * forName() method should be used if you know the fully
             * qualified name of class.This cannot be used for
             * primitive types. r 
             */
            Class<?> classObj = Class.forName("com.Student");
            System.out.println(classObj.getName());
            System.out.println(classObj.getSimpleName());
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}
Output
com.Student
Student

ReflectionDemo2.java
import com.Student;

public class ReflectionDemo2
{
    public static void main(String[] args)
    {
        Student student = new Student("Peter", 25);

        /*
         * Returns:The Class object that represents the runtime class of this
         * object.
         */
        Class<? extends Student> classObj = student.getClass();
        System.out.println(classObj.getName());

    }

}
Output
com.Student

ReflectionDemo3.java
import com.Student;

public class ReflectionDemo3
{
    public static void main(String[] args)
    {
        /*
         * If a type is available but there is no instance then it is possible
         * to obtain a Class by appending ".class" to the name of the type.It
         * can be used for primitive data type also.
         */
        Class<Student> studentClassObj = Student.class;
        System.out.println(studentClassObj.getName());

        Class<Integer> intClassObj = int.class;
        System.out.println(intClassObj.getName());
    }

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/921a65d57c0ec8a07d5edae78f1552619b89e108/BasicJava/ReflectionDemo_get_objectOfClass/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • 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