...

View Full Version : Java default parameter values



JPM
02-12-2005, 10:14 PM
Some time ago I read about default parameter values, like

public blah(int a=0, int b=2) { ... }
or atleast I thought I did, cause it wont work when I try it out.
Ive searched google, but cant find anything.

Am I doing it wrong, or is this not supported in java?

cfc
02-12-2005, 10:38 PM
I don't think you can use default argument values in Java but you can use overloading to achieve a similar effect.

ie.


public int myMethod ()
{
return 0*2;
}

public int myMethod (int a, int b)
{
return a*b;
}

public int myMethod (int x, boolean isA)
{
if (isA)
return x * 2;
else
return 0; // 0 * x
}

JPM
02-12-2005, 10:47 PM
Yea, I know. It's just that I have a Lot of parameters.
Thanks anyway.

KeZZeR
02-13-2005, 01:22 AM
I've never known if it's possible to modify variables in method parameters

cfc
02-13-2005, 01:53 AM
I've never known if it's possible to modify variables in method parameters

The idea isn't to modify them, but to provide defaults in case the variables aren't given. Python implements something like this (http://python.org/doc/2.4/ref/function.html).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum