Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You cannot store types, but you can store the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html" rel="nofollow noreferrer"><code>Class</code></a> object for each class loaded by the JVM. When the JVM loads and initializes a class on the classpath, it creates a single <code>Class</code> object for that class.</p> <p>The <code>Class</code> class gives you access to a number of instance methods for performing <a href="http://docs.oracle.com/javase/tutorial/reflect/" rel="nofollow noreferrer">reflection</a> on your program.</p> <blockquote> <p>Also, how might one do this and how inadvisable is it (and why)?</p> </blockquote> <p>For example</p> <pre><code>List&lt;Class&lt;?&gt;&gt; classes = new ArrayList&lt;&gt;(); classes.add(Example.class); </code></pre> <p>Now you can go through the elements and call different methods depending on what you want to do. If you want to create a new instance (and your class has a no-arg constructor), you can do</p> <pre><code>classes.get(0).newInstance(); </code></pre> <p>If you don't have a no-argument constructor, you will need to get <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html" rel="nofollow noreferrer"><code>Constructor</code></a> references from the <code>Class</code> object.</p> <p>If you need to invoke methods (whether <code>static</code> or not), you need to get a <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html" rel="nofollow noreferrer"><code>Method</code></a> reference. Read through the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html" rel="nofollow noreferrer">javadoc</a>.</p> <blockquote> <p>how inadvisable is it (and why)?</p> </blockquote> <p>There are some things you cannot do without reflection, but it is generally discouraged.</p> <ul> <li><a href="https://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful">What is reflection and why is it useful?</a></li> <li><a href="https://stackoverflow.com/questions/435553/java-reflection-performance">Java Reflection Performance</a></li> <li><a href="https://softwareengineering.stackexchange.com/questions/123956/why-should-i-use-reflection">Why should I use reflection?</a> </li> <li><a href="https://stackoverflow.com/questions/1392351/java-reflection-why-is-it-so-slow">Java Reflection: Why is it so slow?</a></li> <li><a href="https://softwareengineering.stackexchange.com/questions/170778/why-is-javas-reflection-package-considered-the-dark-side-of-the-force">Why is Java's reflection package considered the dark side of the force?</a></li> </ul> <hr> <p>You should try to minimize using Reflection when you can very well use the <code>Factory</code> pattern to achieve your goals in this situation. </p> <p><a href="https://stackoverflow.com/questions/20726652/java-list-of-uninstantiated-classes/20726684?noredirect=1#comment31052503_20726652">User user2864740 suggests</a></p> <blockquote> <p>I would have a list of "factories that know how to create the appropriate object", if practical - really depends upon where this data comes from and what role it has.</p> </blockquote>
 

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