Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 />

  • #2
    cfc
    cfc is offline
    Regular Coder
    Join Date
    Dec 2004
    Location
    Keswick, Ontario
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yea, I know. It's just that I have a Lot of parameters.
    Thanks anyway.
    <JPM />

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've never known if it's possible to modify variables in method parameters

  • #5
    cfc
    cfc is offline
    Regular Coder
    Join Date
    Dec 2004
    Location
    Keswick, Ontario
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •