0
public void getData(int i){
    System.out.println("1");
}
public void getData(Integer i){
    System.out.println("2");
}

The following line of code

this.getClass().getMethod("getData",Integer.class).invoke(this, 10);

prints 2 , how to make it print 1?

1
  • The title is misleading. IMHO something like "Distinguish int and Integer parameters when invoking method via reflection" would be accurate. Commented Jun 6, 2014 at 13:56

1 Answer 1

5

You are requesting the method that accepts an Integer. Change that to the one that takes an int and you are done:

this.getClass().getMethod("getData",int.class).invoke(this, 10);

Note that there as int.class although int is a primitive type. It exists exactly for this reason.

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

2 Comments

The same value is also available as the public static final constant TYPE in the Integer class.
@lpratlong Thanks for the ideone example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.