• Post Reply Bookmark Topic Watch Topic
  • New Topic

pass primitive types using Reflection api  RSS feed

 
Suresh Appagundi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How to pass primitive types, Array types and user define type to a method, when invoking method using Java reflection api.

Any sample code or any related links will be very helpful

thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you worked through the Java Tutorial, specifically the section on reflection? It has a number of examples.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrap them in the corresponding wrapper classes (int -> Integer, char -> Character, etc.) and the reflection API will automatically unwrap them.
 
Suresh Appagundi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper de Jong:
Wrap them in the corresponding wrapper classes (int -> Integer, char -> Character, etc.) and the reflection API will automatically unwrap them.

--------------------------------------------
Suppose i have

public void set(int i){
}

public void set(Integer i){
}

And if i wrap the primitive int into Integer wrapper, which of the above methods will be considered. Actually the second one. But i want the first method to invoke. Now how do i do it.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every method is represented by a distinct "java.lang.reflect.Method" object. You get the Method object for the specific method you want to call, and you invoke it. In other words, you yourself choose between the methods.

If you had worked through the tutorial linked above, you would know this already!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!