📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ 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.
Comments
Post a Comment
Leave Comment