🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Java Reflection allows us to inspect and manipulate classes, fields, methods, and other components at runtime. Enums in Java are special classes that represent a group of constants. Using reflection, we can inspect and interact with enums just like we do with other classes.
Obtaining Enum Information
To use reflection with enums, we first need to obtain the Class object of the enum. Once we have the Class object, we can use various reflection methods to inspect the enum constants, methods, and fields.
Example Enum
Let's start by defining an example enum:
public enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
Using Reflection to Inspect Enums
We can use reflection to obtain information about the enum, such as its constants, methods, and fields.
Example: Inspecting Enum Information
import java.lang.reflect.Method;
public class EnumReflectionExample {
public static void main(String[] args) {
try {
// Obtain the Class object for the enum
Class<?> enumClass = Class.forName("Day");
// Get the enum constants
Object[] constants = enumClass.getEnumConstants();
System.out.println("Enum Constants:");
for (Object constant : constants) {
System.out.println(" - " + constant);
}
// Get the methods of the enum
Method[] methods = enumClass.getDeclaredMethods();
System.out.println("Methods:");
for (Method method : methods) {
System.out.println(" - " + method);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Enum Constants:
- SUNDAY
- MONDAY
- TUESDAY
- WEDNESDAY
- THURSDAY
- FRIDAY
- SATURDAY
Methods:
- public static Day Day.valueOf(java.lang.String)
- public static Day[] Day.values()
Using Reflection with Enums and Custom Methods
Enums in Java can have custom methods and fields. We can use reflection to inspect these custom methods and fields as well.
Example Enum with Custom Methods
public enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
public boolean isWeekend() {
return this == SUNDAY || this == SATURDAY;
}
}
Example: Inspecting Custom Methods in Enum
import java.lang.reflect.Method;
public class CustomEnumReflectionExample {
public static void main(String[] args) {
try {
// Obtain the Class object for the enum
Class<?> enumClass = Class.forName("Day");
// Get the enum constants
Object[] constants = enumClass.getEnumConstants();
System.out.println("Enum Constants:");
for (Object constant : constants) {
System.out.println(" - " + constant);
}
// Get the methods of the enum
Method[] methods = enumClass.getDeclaredMethods();
System.out.println("Methods:");
for (Method method : methods) {
System.out.println(" - " + method);
}
// Invoke custom method on an enum constant
Method isWeekendMethod = enumClass.getMethod("isWeekend");
for (Object constant : constants) {
boolean isWeekend = (boolean) isWeekendMethod.invoke(constant);
System.out.println(constant + " is weekend? " + isWeekend);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Enum Constants:
- SUNDAY
- MONDAY
- TUESDAY
- WEDNESDAY
- THURSDAY
- FRIDAY
- SATURDAY
Methods:
- public boolean Day.isWeekend()
- public static Day Day.valueOf(java.lang.String)
- public static Day[] Day.values()
SUNDAY is weekend? true
MONDAY is weekend? false
TUESDAY is weekend? false
WEDNESDAY is weekend? false
THURSDAY is weekend? false
FRIDAY is weekend? false
SATURDAY is weekend? true
Conclusion
Java Reflection provides a way to inspect and manipulate enums at runtime. By using reflection, we can obtain information about enum constants, methods, and fields, and even invoke custom methods defined in enums. This capability adds a layer of flexibility and introspection to Java applications. For more detailed information on Java Reflection, you can refer to the official Java Reflection API documentation.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment