title: dynamic object creation and assignment using reflection
body: <p>I'm loading a class with following statement:</p>
  
  <pre><code>Class classToLoad = Class.forName("com.somePackage.SomeotherPackage.classname" );
  </code></pre>
  
  <p>Later i wd use reflection to get the methods of this class. now for invoking the methods with methodname.invoke() function i'd require the object of the loaded class. thus i want to create the object of the loaded class. I try to do it this way: </p>
  
  <pre><code>Object obj = classToLoad.newInstance();
  </code></pre>
  
  <p>but the problem in this is that this way i don't get the object of the class loaded but i get object of Object class. </p>
  
  <p>Now if i want to call the functions of the loaded class, i do it like:</p>
  
  <pre><code>methodName.invoke(obj);
  </code></pre>
  
  <p>it throws an exception:</p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: object is not an instance of declaring class </p>
  </blockquote>
  
  <p>can anybody please help? </p>
  
  <p>Update on the problem:</p>
  
  <p>The problem is that i need the left hand side of the assignment to be of a different class type and that class type will be decided on run time</p>
  
  <p>For the below statement:</p>
  
  <p>Object instance = clazz.newInstance();</p>
  
  <p>"instance" should be of the "clazz" type and not the "Object" class.</p>
  
  <p>How can i achieve this?</p>