We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Method default values
Page Index Toggle Pages: 1
Method default values (Read 888 times)
Method default values
May 14th, 2005, 1:17pm
 
Coming from a C++ background, I've sometimes used methods with default values, saving having to write an overloaded version for every possible combination of arguments, is this possible in Java/Processing?

If you're not sure what I mean, then here is an example:

Code:
class foo
{
int bar;
int baz;
foo(int _bar)
{
bar=_bar;
}

void defautlVariableMethod(boolean thing,int _baz=-1)
{
baz=_baz;
}
}


With this you can call foo.defaultVariableMethod(true); and have the value -1 automatically inserted as the value of _baz, or put a value in there if you don't want the default.
Re: Method default values
Reply #1 - May 14th, 2005, 1:59pm
 
As far as I know, it's not possible.  You can of course have multiple methods with the same name, so long as they take different arguments.  So you could have one which only takes a boolean, and calls the full version with a default value for the int.  But you have to define each variation separately.  This is quite common in the standard Java API, especially with constructors (you often see them labelled as "convenience methods") which is why I'm pretty sure there's no syntax for default argument values.

It might have made it into the new Java 1.5 syntax changes along with templating etc. but I don't think it did.

Re: Method default values
Reply #2 - May 14th, 2005, 2:24pm
 
Ah well, looks like I have to write multiple versions of the method. thankfully most of them can just do one thing, then call a different version of the method, so I don't have to have multiple versions of the bulk of the method.
Page Index Toggle Pages: 1