• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Optional argument

 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I make an argument to a method have a optional argument? So that it will use a default value if none is given?
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are different ways to do that, but the way I would probably do it would be to overload a method and call the original method from the overloaded methods. You handle inserting the default values in the overloaded methods, like so:

So if you call the method like so:

In the second one you have inserted your default value!
HTH,
E
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not available yet, but i've heard that the next release of java will allow for optional arguments. something like the "..." in C, you will basically be able to say "and then 0 or more arguments all of type X".
they will all have to be the same type, and be the last ones in your param list.
But again, this is all rumors until the actual product is released.
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++ I could, when declaring the function/method, set a default value for each argument, doesn't seem very clever to have to create multiple methods, but well, seems like I have to
Thank you
 
Vinod Chandana
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,
I really hope they do so. I've heard that the next version of java will include enumerations. That is good to know.
Regards,
Vinod.
Originally posted by fred rosenberger:
It's not available yet, but i've heard that the next release of java will allow for optional arguments. something like the "..." in C, you will basically be able to say "and then 0 or more arguments all of type X".
they will all have to be the same type, and be the last ones in your param list.
But again, this is all rumors until the actual product is released.

[ October 21, 2003: Message edited by: Vinod Chandana ]
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carl Pettersson:
In C++ I could, when declaring the function/method, set a default value for each argument, doesn't seem very clever to have to create multiple methods, but well, seems like I have to
Thank you

Another way to handle this, if you only want to create a single method, is declare the method with your arg list, and when the method is called just pass in a null value(for objects) or some exclusive numeric value (for primitives) for the args you want to be optional, and do some checking in the method. If the optional value(s) is(are) null or, say, -1, just set the value(s) to your default(s). The advantage of the method I suggested above is you can call either one in different contexts(i.e. you may not have all the args present in the calling context) and still get the desired result. It's just a bit more flexible, I guess.
I'm not a C++ coder so I'm not sure what you mean by setting defaults. Do you mean that, if you declare a method with three args, two of which are optional, then you only have to call the method with the non-optional arg and the compiler and linker will know to insert the other ones?
E
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic