Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Defining a method with an optional parameter (http://www.velocityreviews.com/forums/t125970-defining-a-method-with-an-optional-parameter.html)

Ahmed Moustafa 08-24-2003 08:09 PM

Defining a method with an optional parameter
 
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,


Paul Tomblin 08-24-2003 08:15 PM

Re: Defining a method with an optional parameter
 
In a previous article, Ahmed Moustafa <ahmed@NOSPAMarbornet.org> 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 <ptomblin@xcski.com>, 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

Roedy Green 08-24-2003 08:42 PM

Re: Defining a method with an optional parameter
 
On Sun, 24 Aug 2003 20:09:56 GMT, Ahmed Moustafa
<ahmed@NOSPAMarbornet.org> 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.

Tor Iver Wilhelmsen 08-25-2003 04:05 PM

Re: Defining a method with an optional parameter
 
Ahmed Moustafa <ahmed@NOSPAMarbornet.org> 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
}

Dale King 08-25-2003 09:11 PM

Re: Defining a method with an optional parameter
 
"Ahmed Moustafa" <ahmed@NOSPAMarbornet.org> wrote in message
news:CG92b.1817$3E.1700@newsread3.news.pas.earthli nk.net...
> Roedy Green wrote:
> > On Sun, 24 Aug 2003 20:09:56 GMT, Ahmed Moustafa
> > <ahmed@NOSPAMarbornet.org> 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



codemonkey 03-02-2010 11:40 PM

Java ... syntax
 
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)

grizzleybear 04-17-2012 12:34 PM

Quote:

Originally Posted by Ahmed Moustafa (Post 535558)
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?


All times are GMT. The time now is 12:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.