Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Java Reflection is a process of examining or modifying the run time behavior of a class at run time. Java Reflection API is used to manipulate class and its members which include fields, methods, constructor, etc. at runtime. In This article we would understand Java Reflection API in detail.
This article will focus on following pointers:
So let us get started with these pointers in this article on Java Reflection API
The Reflection API is mainly used in:
So what is Class in Java lang reflect package?
Following is a list of various Java classes in java.lang.package to implement reflection-
No let us take a look at Java Reflection API methods,
Method | Description |
public String getName() | returns the class name |
public static Class forName(String className)throws ClassNotFoundException | loads the class and returns the reference of Class class. |
public Object newInstance()throws InstantiationException,IllegalAccessException | creates new instance. |
public boolean isInterface() | checks if it is interface. |
public boolean isArray() | checks if it is array. |
public boolean isPrimitive() | checks if it is primitive. |
public Class getSuperclass() | returns the superclass class reference. |
public Field[] getDeclaredFields()throws SecurityException | returns the total number of fields of this class. |
public Method[] getDeclaredMethods()throws SecurityException | returns the total number of methods of this class. |
public Constructor[] getDeclaredConstructors()throws SecurityException | returns the total number of constructors of this class. |
public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException | returns the method class instance. |
Let us move forward with article,
There are 3 ways to get the instance of Class class. They are as follows:
Let’s see the simple example of forName() method.
class Simple{} class Test{ public static void main(String args[]){ Class c=Class.forName("Simple"); System.out.println(c.getName()); } }
Output:
Simple
It returns the instance of Class class. It should be used if you know the type. Moreover, it can be used with primitives.
class Simple{} class Test{ void printName(Object obj){ Class c=obj.getClass(); System.out.println(c.getName()); } public static void main(String args[]){ Simple s=new Simple(); Test t=new Test(); t.printName(s); } }
Output:
Simple
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 Test{ public static void main(String args[]){ Class c = boolean.class; System.out.println(c.getName()); Class c2 = Test.class; System.out.println(c2.getName()); } }
Output:
boolean
Test
Now let us continue with this Java Reflection API article
Thus we have come to an end of this article on ‘Java Reflection API’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co