File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I... make a random number of arguments call?

 
Petar Thomas
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Whoa, that's what 'random' means, like in 'random acces')

It's not really important (maybe). I'm just learning, and I'm a begginer, and I haven't got to the reflection yet, but I just have ideas , and I wonder if this is possible at all, maybe even how?

So... can I call a method programmatically? I think that probably I can, with reflection. Can I call an arbitrary number of arguments programmatically, or just arbitrary arguments? (not var-args)

Actually I do have some idea that attracts me all the time in that direction, and that's why I ask this question which is way over my current knowledge, so that I can sleep better. Thanks.

: (Like for example that elements of an ArrayList are each solo one argument in a method call. Not that ArrayList is one argument, but arguments become all and each elements separatedly, of an ArrayList, together with the variable number of arguments, call, and the cherry on top. Hm?)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reflection API allows you to discover things about Java classes, such as which constructors, methods and fields a class has, at runtime, and it makes it also possible to call constructors and methods on objects in a dynamic way (i.e. without the need to know beforehand, at compile time, which methods there are).

Have a look at this part of Sun's Java tutorials: Trail: The Reflection API
 
Petar Thomas
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this for the begining, which answers a part of my wondering. ( may-be even all )



This little program is a complete, if minimal, test framework. It takes a list of class names on the command line. For each class name, it instantiates the class using its parameterless constructor and invokes a parameterless method called test. If the instantiation or invocation throws an exception, the test is deemed to have failed. The program prints each failure, followed by a summary of the test results. The reflective instantiation and invocation no longer require explicit array creation, because the getMethod and invoke methods accept a variable argument list. The program also uses the new printf facility, which relies on varargs. The program reads much more naturally than it would without varargs.


From Sun's site.
 
Petar Thomas
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You mister Jesper Young.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic