aspose file tools
The moose likes Java in General and the fly likes pass primitive types using Reflection api Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "pass primitive types using Reflection api" Watch "pass primitive types using Reflection api" New topic
Author

pass primitive types using Reflection api

Suresh Appagundi
Ranch Hand

Joined: Sep 22, 2003
Posts: 44
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
Marshal

Joined: Mar 22, 2005
Posts: 35438
    
    9
Have you worked through the Java Tutorial, specifically the section on reflection? It has a number of examples.


Android appsImageJ pluginsJava web charts
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

Wrap them in the corresponding wrapper classes (int -> Integer, char -> Character, etc.) and the reflection API will automatically unwrap them.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Suresh Appagundi
Ranch Hand

Joined: Sep 22, 2003
Posts: 44
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
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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!


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass primitive types using Reflection api
 
Similar Threads
Reflection doubt
ways of instantiation
Find the data type of a variable
Object.class
equals() vs ==