title: Java Classloaders - Invoking a static method inside a private class
body: <p>I would like to invoke a static method inside of a private class in Java using classloaders.</p>
  
  <p>This is a short version of my classloader that I have been using.</p>
  
  <pre><code>URL[] jarURLArray = { server.lan.serverJAR().toURL() };
  URLClassLoader serverClassLoader = new URLClassLoader(jarURLArray,  this.getClass().getClassLoader());
  Class mainClass = Class.forName("com.packagename.someclass", true, serverClassLoader);
  Class sampleArgClass[] = { (new String[1]).getClass() };
  Method mainMethod = mainClass.getDeclaredMethod("getSimplifiedName", sampleArgClass);
  Object mainMethodInstance = mainClass.newInstance();
  Object serverArgConverted[] = { args };
  Object result = mainMethod.invoke(mainMethodInstance, serverArgConverted);
  </code></pre>
  
  <p>This code loads classes from a jar file and I am able to invoke classes under normal circumstances.</p>
  
  <p>When I have a class, such as this one:</p>
  
  <pre><code>public final class someClass
  {
  private static Server server;
  
  /**
   * Static class cannot be initialized.
   */
  private someClass()
  {
  }
  
  public static int someValue()
  {
      return someValue;
  }
  </code></pre>
  
  <p>I am unable to reach the someValue() method because of how the classloader creates new instances of the class, which, is not possible because it has a private constructor.</p>
  
  <p>How can I reach the someValue method using a classloader?</p>