Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Java Reflection is a powerful mechanism that allows you to inspect and manipulate classes, interfaces, constructors, methods, and fields at runtime. It can be useful for tasks such as object serialization, deserialization, and dynamic method invocation. In this tutorial, we will cover the basics of Java Reflection and provide examples of how to use it.
You can access a Class
object by calling the getClass()
method on an object or using the .class
notation on a class name. You can then retrieve information about the class, such as its name, superclass, and implemented interfaces.
Example:
public class Main { public static void main(String[] args) { String str = "Hello, World!"; Class<?> stringClass = str.getClass(); System.out.println("Class name: " + stringClass.getName()); System.out.println("Superclass name: " + stringClass.getSuperclass().getName()); Class<?>[] interfaces = stringClass.getInterfaces(); for (Class<?> iface : interfaces) { System.out.println("Implemented interface: " + iface.getName()); } } }
You can access the constructors of a class using the getConstructors()
and getConstructor()
methods of the Class
object.
Example:
import java.lang.reflect.Constructor; public class Main { public static void main(String[] args) { Class<?> stringClass = String.class; Constructor<?>[] constructors = stringClass.getConstructors(); for (Constructor<?> constructor : constructors) { System.out.println("Constructor: " + constructor); } } }
You can access the methods of a class using the getMethods()
and getMethod()
methods of the Class
object.
Example:
import java.lang.reflect.Method; public class Main { public static void main(String[] args) { Class<?> stringClass = String.class; Method[] methods = stringClass.getMethods(); for (Method method : methods) { System.out.println("Method: " + method); } } }
You can access the fields of a class using the getFields()
and getField()
methods of the Class
object.
Example:
import java.lang.reflect.Field; public class Main { public static void main(String[] args) { Class<?> stringClass = String.class; Field[] fields = stringClass.getFields(); for (Field field : fields) { System.out.println("Field: " + field); } } }
You can create new instances of a class using the newInstance()
method of the Constructor
object, and invoke methods using the invoke()
method of the Method
object.
Example:
import java.lang.reflect.Constructor; import java.lang.reflect.Method; public class Main { public static void main(String[] args) throws Exception { Class<?> stringClass = String.class; // Create a new instance of the String class Constructor<?> constructor = stringClass.getConstructor(String.class); Object newInstance = constructor.newInstance("Hello, World!"); // Invoke the "length" method on the new instance Method lengthMethod = stringClass.getMethod("length"); int length = (int) lengthMethod.invoke(newInstance); System.out.println("Length: " + length); } }
In this tutorial, we covered the basics of Java Reflection, including accessing class information, constructors, methods, and fields, as well as creating instances and invoking methods.