Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2005, 09:14 PM   PM User | #1
JPM
Regular Coder

 
Join Date: Mar 2004
Location: Norway
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
JPM is an unknown quantity at this point
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?
__________________
<JPM />
JPM is offline   Reply With Quote
Old 02-12-2005, 09:38 PM   PM User | #2
cfc
Regular Coder

 
Join Date: Dec 2004
Location: Keswick, Ontario
Posts: 251
Thanks: 0
Thanked 0 Times in 0 Posts
cfc is an unknown quantity at this point
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
}

Last edited by cfc; 02-12-2005 at 09:45 PM..
cfc is offline   Reply With Quote
Old 02-12-2005, 09:47 PM   PM User | #3
JPM
Regular Coder

 
Join Date: Mar 2004
Location: Norway
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
JPM is an unknown quantity at this point
Yea, I know. It's just that I have a Lot of parameters.
Thanks anyway.
__________________
<JPM />
JPM is offline   Reply With Quote
Old 02-13-2005, 12:22 AM   PM User | #4
KeZZeR
Regular Coder

 
Join Date: Oct 2004
Location: England
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KeZZeR is an unknown quantity at this point
I've never known if it's possible to modify variables in method parameters
KeZZeR is offline   Reply With Quote
Old 02-13-2005, 12:53 AM   PM User | #5
cfc
Regular Coder

 
Join Date: Dec 2004
Location: Keswick, Ontario
Posts: 251
Thanks: 0
Thanked 0 Times in 0 Posts
cfc is an unknown quantity at this point
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.
cfc is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:53 AM.


Advertisement
Log in to turn off these ads.