Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding an Enumeration Class using Reflection in Java
    primarykey
    data
    text
    <p>I think I need some help with finding an enumeration class within another class using reflection in Java. I have been battling with this for far too long now. I have read this as well as a number of other posts and they all make me believe it should work as below.</p> <pre><code>public class ModelActivity { public enum AttributeEnumeration { MODELID, MODELURGENCY, MODELDUEDATEANDTIME } public static void main(String[] args) { // Find the class with the given name String className = "ModelActivity"; Class modelClass = null; try { // Retrieve the Class with the given className... modelClass = Class.forName(className); } catch (ClassNotFoundException e) { throw new RuntimeException("Class by name '" + className + "' not found.", e); } // Find the AttributeEnumeration within the class String attributeEnumerationClassName = className + ".AttributeEnumeration"; Class attributeEnumerationClass = null; try { attributeEnumerationClass = Class.forName(attributeEnumerationClassName); } catch (ClassNotFoundException e) { throw new RuntimeException("Class by name '" + attributeEnumerationClassName + "' not found.", e); } } } </code></pre> <p>However, what actually happens is that the modelClass is found correctly, but the attributeEnumerationClass is not, that is, I get the second ClassNotFoundException as follows:</p> <pre><code>Exception in thread "main" java.lang.RuntimeException: Class by name 'ModelActivity.AttributeEnumeration' not found. at ModelActivity.main(ModelActivity.java:27) Caused by: java.lang.ClassNotFoundException: ModelActivity.AttributeEnumeration at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at ModelActivity.main(ModelActivity.java:25) </code></pre> <p>Could anyone please point the--probably obvious--mistake out to me. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload