CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Java default parameter values (http://www.codingforums.com/showthread.php?t=52246)

JPM 02-12-2005 09:14 PM

Java default parameter values
 
Some time ago I read about default parameter values, like
Code:

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 09: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.
Code:

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 09:47 PM

Yea, I know. It's just that I have a Lot of parameters.
Thanks anyway.

KeZZeR 02-13-2005 12:22 AM

I've never known if it's possible to modify variables in method parameters

cfc 02-13-2005 12:53 AM

Quote:

Originally Posted by KeZZeR
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.


All times are GMT +1. The time now is 12:30 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.