

Reflection API provides us information about the class to which an object belongs and also the members of that class which can be accessed by using the object.Java Reflection API uses two packages namely java.lang and to introspect and modify the runtime behavior of a Java application during runtime.Static void method1() throws ClassNotFoundException ) Technique 1: Using forName() method of Class class

TestReflection.java package techguru.examples At the same time we would also like to determine if the specified Classobject represents an interface type or class type by using isInterface() method of.
#JAVA REFLECTION TUTORIAL FOR BEGINNERS HOW TO#
The following example demonstrates how to get the instance of Classclass in different ways. NOTE:The getClass() method of is an important method which returns the runtime class of an object and its return type is Class. In addition to these methods, the classes in the package also have some useful methods for performing introspection and analysis. getField(), getFields(), getDeclaredFields().getMethod(), getMethods(), getDeclaredMethods().getConstructor(), getConstructors(), getDeclaredConstructor().To these three useful methods the Reflection API adds some additional methods to class Class. newInstance(), which invokes the null constructor on the class (if it exists) and returns us an object instance of that class of object.GetName(), which returns the name of the class as a Stringobject, which is useful for identifying object references by their class names. We must provide the fully-qualified class name here. The three most important methods in this class are:įorName(), which loads a class of a given name, using the current class loader.

Class loaders in the JVM return objects of type Class. The special class Classis the universal type for the meta information that describes objects within the JVM.

#JAVA REFLECTION TUTORIAL FOR BEGINNERS CODE#
