File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

optional inputs and defaults to method

 
P. Ingle
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Wingate
Ranch Hand
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P. Ingle
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
P. Ingle
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic