Defining a method with an optional parameter

A

Ahmed Moustafa

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,
 
P

Paul Tomblin

In a previous article 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);
}
 
R

Roedy Green

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.
 
T

Tor Iver Wilhelmsen

Ahmed Moustafa 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?

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
}
 
Joined
Mar 2, 2010
Messages
2
Reaction score
0
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)
 
Joined
Apr 17, 2012
Messages
1
Reaction score
0
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top