aspose file tools*
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 Spring in Action this week in the Spring 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to set a default value for a method parameter?