How can I find out what enums are defined by a class?

I know I can get the public static members of a class by doing something like:


but this doesn't get me the enums. I'd like to be able to get them from the Class object returned by the getClass method. Any ideas?

Asked by: Michael824 | Posted: 23-01-2022

Answer 1

(Turned into a community wiki as it looks like there's scope for a fair amount of expansion, e.g. to include tackline's comments. No sense in me just transcribing comments when everyone could be expanding it.)

Do you mean enums nested within a top-level class? If so, use Class.getDeclaredClasses() and iterate through the results seeing if any of the nested classes are enums. The simplest way of testing each nested class is to use Class.isEnum(); if you want to iterate through the values within the enum then Class.getEnumConstants() is the way to go.

Answered by: Oliver779 | Posted: 24-02-2022

Answer 2


Answered by: Fenton248 | Posted: 24-02-2022

Similar questions

java - JMX Defined

How do I use user defined Java classes within Matlab?

I have read the documentation and several websites on exactly how to do this, however Matlab does not seem to pick up the classes that I have added to the dynamic java class path. Nor do I use the right syntax to correctly construct the object. I have an class HandDB and which to create an object of this type and invoke it's static methods to connect to a SQL database. The class has an empty constructor and takes n...

Using user defined method to print line to console in java

I have simple program like this class println { public static void main(String[] args) { System.out.println("Hello World!"); } } But i dont want to use System.out.println to print..i want to use my own user defined method to print the data.

java - User defined library

I am a newbie at J2ME. Recently, I've shifted from Netbeans to Eclipse because of some internal constraints. I am making an App which makes use of a user-defined library. This library has only a jar file. Now, in Netbeans there were no problems. But in eclipse, I am unable to access the classes defined in the jar file included in my library. The exception thrown is java.lang.NoClassDefFoundError. What should I do?

java - Why do I get error "prefix [..] is not defined" when I try to use my jsf custom tag?

I created a jsf custom tag (I'm not sure that it's correct, I could miss something easily, so I attached code below). Now I'm trying to use this tag but I get an error: error on line 28 at column 49: Namespace prefix gc on ganttchart is not defined So, here is the xhtml-page: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:...

java - How to get user defined system colors on Linux?

How do I get correct colors associated with my KDE or Gnome profile on Linux? java.awt.SystemColor seems to give me root (?) account colors, at least colors definitions doesn't match my settings (whereas on Windows everything is fine). Are there any APIs that let me access current user's Window Manager color settings from Java?

Write a text file with a defined maximum lines count in java

Hi all Is it possible to write a .txt file in java with a certain number of lines? Pratically I have to write a file in append mode, for example with a maximum of 4 lines: File .txt 1 item1 item2 item3 item4 When the files reaches 4 lines, writing on it can't be allowed and a new file is generated File .txt 2 item5 item6 .... Thanks

java - How to access a thread defined in another class?

I have encountered a problem when I want to do a time counting. Basicly the problem is like this: there is a class A, which initiates a private thread in itself, and I have a instant of A in my class B, and in the main method of B I invoked some methods of A and want to test the time to run these methods. A a = new A(); //start time counter for (int i = 0; i < 10; i++){ invoke a.method() that takes some ...

java - How is the OSGi bundle start level defined?

How is the OSGi bundle start level defined? I am using Apache felix and would like to persist the start level across framework executions. I do not anticipate a need to change the start level of a bundle very frequently at all an an entry in Manifest.MF seems the most sensible. I've to org.osgi.framework.startlevel but have not seen a practical example. I am also using maven with the maven-bundle-plugin, i...

user defined data type in mysql with java

lets say i have a class named myClass1 and i have another class myClass2 such that myClass1 contains a collection of myClass2... storing this in java is very easy. i was wondering how can we do that if the values needs to be stored in a table in mysql? i m trying to construct a table so that i can do something like this: CREATE table myClass1(attr1 integer, attri2 char(), attri3 varchar(20), attri4 ...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)