aspose file tools*
The moose likes Beginning Java and the fly likes optional inputs and defaults to method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "optional inputs and defaults to method" Watch "optional inputs and defaults to method" New topic
Author

optional inputs and defaults to method

P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
Hello,

Does Java allow having optional inputs to methods and a way to set defaults for those attributes if the optional input is not present.
for example:

class myClass{

String attr1;
int attr2;

void someMethod(String inAttr1, Optional int inAttr2 = 5){

/* Idea here is to have inAttr2 optional input which will de defaulted to value 5 */

//method bode do something here......

}
}

I know (c++) or c# .net allows it (my c# like syntax may be wrong, but you get the idea.)

P.Ingle


Thanks,<br />P.Ingle
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Java doesn't allow you to specify default values to parameters ina method declaration. You can however accomplish the same task through the following design pattern:



Fun programming etcetera!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Java 5 (Tiger, JDK 1.5, whatever you want to call it!) has varargs, or variable-length argument lists. This lets you do what Dave recommends in another way:



This single function can be called with one, two (or more) arguments; the second one has a default value.


[Jess in Action][AskingGoodQuestions]
P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
well, I am developing with Java 1.4 so no all the nice stuff that comes with Java 1.5 for me.

My class had 3 attributes, all of which can be optional. So, I have to create multiple constructors with different combinations of inputs to handle all cases.

The optional input with default setting would have been handy.

Thanks for you inputs.
P.Ingle
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
If your class has three class variables of which all are optional, I would reconsider the design. Take a look at the decorator pattern.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

if all three are optional, and all three are ints, how will you know which of the three are being passed in if you get less than three?

You can do it if all three have different types (i.e. one's an int, one's a long and one's a short), but otherwise you're going to have problems...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
you guys are right, I redesigned it - it has 3 attributes - only 2 of which are optional and is working fine:
Thanks for all y'r input.
P.Ingle
--------------------------------------------------------
class MyClass{
String str1; // not optional
String str2; // optional
int int1; // optional

MyClass(){
// no-arg constructor
super();
}

MyClass(String inStr1, String inStr2, int inInt){
this();
str1 = inStr1;
str2 = inStr2;
int1 = inInt;
}

MyClass(String inStr1, String inStr2){
this();
str1 = inStr1;
str2 = inStr2;
}

MyClass(String inStr1, int inInt){
this();
str1 = inStr1;
int1 = inInt;
}

}

[ May 24, 2005: Message edited by: p Ingle ]
[ May 24, 2005: Message edited by: p Ingle ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If I'm not mistaken, the varargs feature in Java 5.0 basically just packages the args in a single array. So you could accomplish the same thing using a previous version of Java by specifying an array as the argument type, and just doing your own packaging.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
You can however accomplish the same task through the following design pattern:


code:
--------------------------------------------------------------------------------

public void myMethod(int a, int b) {// do something useful with a & b} public void myMethod(int a) {// use a default value of 5 for parameter bmyMethod(a, 5);}

--------------------------------------------------------------------------------

what's its name?


java amateur
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: optional inputs and defaults to method