Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use the full class name as in <code>package.ClassName</code>, reflection, and <code>Class.forName(theName)</code>.</p> <p>For instance with a <code>String</code> object: </p> <pre><code>try { String newString = (String)Class.forName("java.lang.String").newInstance(); } catch (IllegalAccessException iae) { // TODO handle } catch (InstantiationException ie) { // TODO handle } catch (ClassNotFoundException cnfe) { // TODO handle } </code></pre> <p>So your method could roughly look like: </p> <pre><code>@SuppressWarnings("unchecked") public static &lt;T&gt; T getInstance(String clazz) { // TODO check for clazz null try { return (T)Class.forName(clazz).getMethod("getInstance", (Class&lt;?&gt;[])null).invoke(null, (Object[])null); } catch (ClassNotFoundException cnfe) { // TODO handle return null; } catch (NoSuchMethodException nsme) { // TODO handle return null; } catch (InvocationTargetException ite) { // TODO handle return null; } catch (IllegalAccessException iae) { // TODO handle return null; } } </code></pre> <p><strong>Edit</strong> for OP:</p> <p>The <code>(Class&lt;?&gt;[])null</code> and <code>(Object[])null</code> are <code>null</code> arguments cast as their expected type. </p> <p>Basically: </p> <ul> <li>the <code>getMethod</code> method of <code>Class</code> takes a <code>String</code> representing the method's name, and a varargs of <code>Class&lt;?&gt;</code> representing its parameter types. The method we call (<code>getInstance</code>) takes no arguments, therefore the argument to <code>getMethod</code> is null, but we want to cast it as the expected argument. More info <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethod%28java.lang.String,%20java.lang.Class...%29" rel="nofollow">here</a>.</li> <li>the <code>invoke</code> method of <code>Method</code> takes an <code>Object</code> as the target instance to invoke the method against (in our case, <code>null</code> since it's a class method) and a varargs of <code>Object</code> as the arguments. But again, your <code>getInstance</code> method takes no arguments, so we use <code>null</code> and cast it as an array of <code>Object</code>. More info <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html#invoke%28java.lang.Object,%20java.lang.Object...%29" rel="nofollow">here</a>.</li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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