This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Is it possible to set a default value for a method parameter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it possible to set a default value for a method parameter?" Watch "Is it possible to set a default value for a method parameter?" New topic
Author

Is it possible to set a default value for a method parameter?

Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
In languages such as C++, a default value can be included as part of the method declaration:

void Process(Employee employee, bool bonus = false)

This method can be called either with:

a.Process(employee, true);

or

a.Process(employee);

in the second case, the parameter bonus is set to false.

Dose Java support this feature?


SCJP 5.0 - 98% (2007)<br />SCWCD 1.4 - 97% (2007)
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Jon Lee:
In languages such as C++, a default value can be included as part of the method declaration:

void Process(Employee employee, bool bonus = false)

This method can be called either with:

a.Process(employee, true);

or

a.Process(employee);

in the second case, the parameter bonus is set to false.

Dose Java support this feature?


Not directly. You can achieve this result by overloading.



This should let you do similar calls. Sure, its more tedious than in C++, but afaik, this is the only way.


ASCII silly question, Get a silly ANSI.
Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
So I have to use overload methods...
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Jon Lee:
So I have to use overload methods...


Yes.
 
 
subject: Is it possible to set a default value for a method parameter?