• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Optional Parameters

 
Jay Goodfellow
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Goodfellow
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic