Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Defining a method with an optional parameter

Reply
Thread Tools

Defining a method with an optional parameter

 
 
Ahmed Moustafa
Guest
Posts: n/a
 
      08-24-2003
Is there a way to define a method with an optional parameter? Or, is it
must to define the method one time with the optional parameter and
another without?

Thanks in advance,

 
Reply With Quote
 
 
 
 
Paul Tomblin
Guest
Posts: n/a
 
      08-24-2003
In a previous article, Ahmed Moustafa <(E-Mail Removed)> said:
>Is there a way to define a method with an optional parameter? Or, is it
>must to define the method one time with the optional parameter and
>another without?


I usually define it once without as a one liner that just calls the one
with the parameter with a default value. I'll even do it in constructors
like that:

public DBScreenDay(Connection conn, int screen, Date date, long dayDuration)
{
this(conn, screen, date, dayDuration, -1);
}


--
Paul Tomblin <(E-Mail Removed)>, not speaking for anybody
As far as the laws of mathematics refer to reality, they are not
certain, and as far as they are certain, they do not refer to reality.
-- Albert Einstein
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-24-2003
On Sun, 24 Aug 2003 20:09:56 GMT, Ahmed Moustafa
<(E-Mail Removed)> wrote or quoted :

>Is there a way to define a method with an optional parameter? Or, is it
>must to define the method one time with the optional parameter and
>another without?


Not in Java, but in other languages that produce byte code. You need
to define two methods, one with the parameter and one without. The
one without often is implement as a call to the one with, supplying a
default value.

The technique falls apart if you want many parms and many default
values.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-25-2003
Ahmed Moustafa <(E-Mail Removed)> writes:

> Is there a way to define a method with an optional parameter? Or, is
> it must to define the method one time with the optional parameter and
> another without?


You use overloading to do this, e.g.

public void foo(int x, int y) {
// Do stuff
}

public void foo(int y) {
foo(0, y); // Implicit x = 0
}
 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      08-25-2003
"Ahmed Moustafa" <(E-Mail Removed)> wrote in message
news:CG92b.1817$(E-Mail Removed) nk.net...
> Roedy Green wrote:
> > On Sun, 24 Aug 2003 20:09:56 GMT, Ahmed Moustafa
> > <(E-Mail Removed)> wrote or quoted :
> >
> >
> >>Is there a way to define a method with an optional parameter? Or, is it
> >>must to define the method one time with the optional parameter and
> >>another without?

> >
> >
> > Not in Java, but in other languages that produce byte code. You need
> > to define two methods, one with the parameter and one without. The
> > one without often is implement as a call to the one with, supplying a
> > default value.

>
> Is it coming in 1.5?


Quoting Joshua Bloch from the article:
http://developer.java.sun.com/develo...ve/2003/jl0729.
html

"I don't think we'll ever add support for default parameter values for
constructors or methods. Overloading provides similar functionality, and we
don't like having many ways of doing the same thing."

--
Dale King


 
Reply With Quote
 
codemonkey codemonkey is offline
Junior Member
Join Date: Mar 2010
Posts: 2
 
      03-02-2010
It's not exactly "optional parameters", but the Java does allow an arbitrary number of arguments as part of an array:

See "Arbitrary Number of Arguments" on sun's web-site (I'd post the link, but the site won't let me)
 
Reply With Quote
 
grizzleybear grizzleybear is offline
Junior Member
Join Date: Apr 2012
Posts: 1
 
      04-17-2012
Quote:
Originally Posted by Ahmed Moustafa View Post
Is there a way to define a method with an optional parameter? Or, is it
must to define the method one time with the optional parameter and
another without?

Thanks in advance,
i gues it is an old post, but I recently stumbled upon this area so wanted to share my idea:

You can always use Java option parameter expression, for example:

Code:
 
Public Constructor (String arg1, Int arg2, String... arg3)
You gotta remember that arg3 is expected to be an array. However, if you know that it is gonna be only one element i.e. arg3[0] you can simply do the following in your constructor definition body:
Code:
this.arg3 = arg3[0];
With regards to your second question, YES you can do that too. But assuming that you want a seamless interface between client and server (sorry, I am too hooked into Client-Server model) you wanna be able to keep the number of constructors to minimum. It is not a MUST for your design, but certainly a good practice.

Does this answer your question?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string as optional parameter for method? Varun Goel Ruby 1 05-14-2008 03:51 PM
defining a method that could be used as instance or static method Sam Python 3 03-10-2008 04:07 PM
Usage of Use="Optional" on a web method parameter... Sinex ASP .Net Web Services 0 05-01-2007 07:11 AM
How to make a "long&" parameter optional Otto Wyss C++ 6 11-09-2004 07:32 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM



Advertisments