aspose file tools*
The moose likes Beginning Java and the fly likes Add arguments to the method @ Runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Add arguments to the method @ Runtime" Watch "Add arguments to the method @ Runtime" New topic
Author

Add arguments to the method @ Runtime

Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

In JAVA is it possible to add arguments in methods @ Runtime? Please provide me reason also.
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

In what situation would you want that?

It is possible to have an undefined number of arguments if you make use of arrays (or collections).

Cheers
Wim
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Or use varargs, available since Java 5.0. These arguments must be all of the same type (or at least have a common super type), the varargs parameter must be the last parameter of the method, and there can be only one varargs parameter per method. Inside the method you treat the parameter as if it's an array. For instance:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Wim Vanni wrote:In what situation would you want that?

It is possible to have an undefined number of arguments if you make use of arrays (or collections).

Cheers
Wim



Actually i was asked this question in interview, so even i don't have any idea that in what type of situation they want.

Thanks Wim....
Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Rob Spoor wrote:Or use varargs, available since Java 5.0. These arguments must be all of the same type (or at least have a common super type), the varargs parameter must be the last parameter of the method, and there can be only one varargs parameter per method. Inside the method you treat the parameter as if it's an array. For instance:


Thanks for the reply Rob.

The Same answer came into my mind. But if there are different types of arguments and we want to add another different type of argument.
e.g. void TestMethod(int arg1, char arg2) and what if i want to add string argument at runtime (e.g. void TestMethod(int arg1, char arg2, String arg3))

what i think is it's not possible but even i want to clear this confusion.

Thanks again.
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96



Should work, no?

The toughtest part is having code that is dynamis enough to handle them, I guess.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add arguments to the method @ Runtime