Java
Reflection
Get class information at runtime.
By EZ4Code Team
reflectionreflection
Code
import java.lang.reflect.*;
Class<?> clazz = String.class;
// Get class name
System.out.println(clazz.getName());
System.out.println(clazz.getSimpleName());
// Get fields
for (Field field : clazz.getDeclaredFields()) {
System.out.println(field.getName() + ": " + field.getType());
}
// Get methods
for (Method method : clazz.getDeclaredMethods()) {
System.out.println(method.getName());
}
// Dynamically create instance
Object obj = clazz.getConstructor(String.class).newInstance("Hello");
// Dynamically invoke method
Method m = clazz.getMethod("length");
int len = (int) m.invoke(obj);Explanation
Reflection allows inspecting class structure and dynamically operating on objects at runtime.
More Java Snippets
String Operations and StringBuilder
Manipulate strings with split, join, substring, and StringBuilder in Java.
Stream API
Process collections using the Stream API.
Lambda Expressions
Simplify code with Lambda expressions.
Optional
Handle null values elegantly.
Collection Operations
Common operations on List, Set, and Map.
Exception Handling
try-catch-finally and custom exceptions.