• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Optional Parameters

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic