aspose file tools*
The moose likes Beginning Java and the fly likes Optional argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Optional argument" Watch "Optional argument" New topic
Author

Optional argument

Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
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

Joined: Oct 26, 2000
Posts: 188
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


My theory of evolution is that Darwin was adopted. - Steven Wright
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10923
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
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

Joined: Aug 26, 2003
Posts: 59
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

Joined: Oct 26, 2000
Posts: 188
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Optional argument
 
Similar Threads
AmbiguosMethodCall
how to handle a NULL args[] value
specifying character set when compiling
create jar problem: jarFile.jar: no such file or directory
Getting java.sql.SQLException. Why?