Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Optional Parameters?

Thread view: 
tomb - 17 Sep 2006 15:31 GMT
Excuse my ignorance - I'm coming from the other development environment :(
Is there a way to specify a function parameter as being optional?  I
know C++ has them, and PHP parameters are all optional, but I can't find
anything about it in Java.  Thanks in advance.

Tom
Patricia Shanahan - 17 Sep 2006 15:53 GMT
> Excuse my ignorance - I'm coming from the other development environment :(
> Is there a way to specify a function parameter as being optional?  I
> know C++ has them, and PHP parameters are all optional, but I can't find
> anything about it in Java.  Thanks in advance.
>
> Tom

You can provide two or more method declarations with the same name but
different parameter lists.

Often, the ones with the shorter lists just call the full version with
default values of the missing parameters, and only the full version has
a non-trivial implementation.

See, for example, the indexOf methods in String. The fromIndex
parameters can be thought of as being optional parameters, although
there are actually separate method declarations with and without them.

Patricia
tomb - 17 Sep 2006 19:06 GMT
Thank you :)

Tom

>> Excuse my ignorance - I'm coming from the other development
>> environment :(
[quoted text clipped - 16 lines]
>
> Patricia
Ian Shef - 18 Sep 2006 20:59 GMT
Patricia Shanahan <pats@acm.org> wrote in news:HjdPg.2625$UG4.1603
@newsread2.news.pas.earthlink.net:

>> Excuse my ignorance - I'm coming from the other development environment :(
>> Is there a way to specify a function parameter as being optional?  I
[quoted text clipped - 5 lines]
> You can provide two or more method declarations with the same name but
> different parameter lists.
<snip>
> Patricia

Starting in Java 1.5, of course, there is also the varargs feature.  See for
example:

http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html

http://java.sun.com/developer/JDCTechTips/2005/tt0104.html#1

To be used sparingly and appropriately as these articles point out.

Signature

Ian Shef     805/F6      *    These are my personal opinions    
Raytheon Company         *    and not those of my employer.
PO Box 11337             *
Tucson, AZ 85734-1337    *



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.