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

Optional Parameters

Jay Goodfellow
Greenhorn

Joined: Aug 26, 2003
Posts: 2
Hi! How can I implement optional parameters for a method?
I know how you can do overloaded parameters, but I want 2 or 3 parameters to be optional for this method. How do I do that? I know in VB you just use the word optional...
Thanks a lot!
Jay
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Jay,
Welcome to JavaRanch!
The Java language doesn't support optional parameters; using overloaded methods for each of the combinations is the only possibility, and of course that only works in the most general sense if the parameters have different types.


[Jess in Action][AskingGoodQuestions]
Jay Goodfellow
Greenhorn

Joined: Aug 26, 2003
Posts: 2
Thanks! I guess I can pass in null and check for it in the method then...

Originally posted by Ernest Friedman-Hill:
Hi Jay,
Welcome to JavaRanch!
The Java language doesn't support optional parameters; using overloaded methods for each of the combinations is the only possibility, and of course that only works in the most general sense if the parameters have different types.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that with the coming Java 1.5, we will be able to declare that a method takes a variable number of arguments. The way it will work probably won't be exactly as you've described you'd like to be able to program in your initial post of this thread, but it might prove useful.
Now, those fancy talkin' folks over in The OO, Patterns, UML and Refactoring Forum might have some advice for you on a better or different design to consider for your situation. Mosey on over there and ask if you like.
Good luck.
[ August 27, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Optional Parameters