2
import java.util.*;
import java.text.*;
import java.lang.reflect.*;

public class Test {
String name;
public Test()
{
System.out.println("In Construtor");
}
public Test(String name)
{
this.name=name;
System.out.println("In Construtor paramitarized-----"+name);
}

public int q() {
    System.out.println("working");
    return 1;

}
public static void main(String args[] ) throws Exception {
    Class c=Class.forName("Test");
    Test t=(Test)c.newInstance();
    t.q();
    Constructor cons[]=c.getConstructors();
    for(Constructor ci:cons)
    ci.newInstance();
    Constructor<?> pcon=c.getConstructor(String.class);
    pcon.newInstance();
    System.out.println(Test.class.getName());
    }
}

I was just trying grasp the concepts of Class and ClassLoader Classes. Now for this line Constructor<?> pcon=c.getConstructor(String.class); its showing that IllegalArgumentException: why?

Thank you Jon Skeet :) I changed my code with this and its working

Class c=Class.forName("Test");
        Test t=(Test)c.newInstance();
        t.q();


        Constructor<?> pcon=c.getConstructor(String.class);
        pcon.newInstance("arijit");
        System.out.println(Test.class.getName());

But When I am compiling with -Xlint ,its giving below mentioned warning: Uncheck call getConstructor(java.lang.Class....) as a member of raw type java.lang.Class

What does this mean?

1 Answer 1

8

You've misdiagnosed it. That's not the statement which is throwing an exception - this is:

ci.newInstance();

You're trying to call that for both constructors (due to the for loop) - so it'll be fine when it calls the parameterless constructor, but not when it calls the one with a String parameter. You'd have to change that call to pass in the right number of arguments depending on which constructor ci refers to at the time.

You have the same problem later:

pcon.newInstance();

This time you know there's a String parameter (you've just asked for a constructor with a string parameter) so you absolutely know that you should provide a String argument, e.g.

pcon.newInstance("foo");

Additionally, you should try to work out why you thought it was the getConstructor() call which was failing. The exception should have given you a stack trace with the right line on it... it's important to be able to diagnose problems accurately, so it's worth looking at what went wrong this time.

Sign up to request clarification or add additional context in comments.

7 Comments

The problem is that he's calling ci.newInstace() with every constructor.
@TedHopp: Indeed. Right statement, right reason - just missed the fact that it might already have worked once before the failure :)
Thank you very much. I changed my code like this and its working now. Class c=Class.forName("Test"); Test t=(Test)c.newInstance(); t.q(); Constructor<?> pcon=c.getConstructor(String.class); pcon.newInstance("arijit"); System.out.println(Test.class.getName());
@Jon Skeet But When I am compiling with -Xlint ,its giving below mentioned warning: Uncheck call getConstructor(java.lang.Class....) as a member of raw type java.lang.Class What does this mean?
Your c variable is just of type Class, which is a raw type. Use Class<?> and I suspect the error will go away.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.