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 › Class default values
Page Index Toggle Pages: 1
Class default values (Read 1051 times)
Class default values
Feb 25th, 2010, 9:36am
 
is there a way of making default values in a class, so passing those variables are optional in a constructor?

in ActionScript you would do something like:

Code:

myClass {
...
myClass (int x, int y, boolean doIt = true) {
....
}
}


so you would have the option of declaring:
dot = new myClass (1,2);
or
dot = new myClass (1,2, false);


i know that you can declare the class twice, without the addittional value, but it looks really messy if you've got a lot of variables floating around, and a lot of doubling up of code:

Code:

myClass {
...
myClass (int x, int y, boolean doIt = true) {
....
}
myClass (int x, int y) {
....
}
}



is there a neater way of doing this?
Re: Class default values
Reply #1 - Feb 25th, 2010, 11:31am
 
thats the way i always did it, dont need it that often... but would be good to know if there is another way.
Re: Class default values
Reply #2 - Feb 25th, 2010, 2:12pm
 
Code:
myClass {
 int x;
 int y;
 int doIt = true;
 myClass(int _x, int _y, boolean _doIt) {
   this(_x, _y);
   doIt = _doIt;
 }
 myClass(int x, int y) {
   x = _x;
   y = _y;
   // doIt has default value defined in declaration
 }
}

Java relies on function overloading/polymorphism, so has no optional parameters. The resulting code is heavy, so we usually offer a limited number of similar functions...
Another way is just to set fields after object creation, directly or with setters (setX(5)).
Re: Class default values
Reply #3 - Feb 26th, 2010, 1:10am
 
Hi,
No answer to your questions (-I use setter-functions as well and keep my constructors small-) but a question:

Can somebody explain me the header

myClass (int x, int y, boolean doIt = true) {....}

?
What does the "= true" do in the declaration, as I thought this will get the value from the call whereas otherwise the other header (with just two parameters) is used.
Re: Class default values
Reply #4 - Feb 26th, 2010, 1:14am
 
If you make your "setter" methods return the object reference (instead of being void), you can chain the object creation with setting. I don't know if this is "good practice", but it seems to work, and may be handy in situations where you want a new object "right here right now" (eg in the middle of some expression, as an argument to some other method, etc).

Code:
void setup() {
Doer doer = new Doer().x(20).y(30).doIt(false);
println("doer.x: " + doer.x);
println("doer.y: " + doer.y);
println("doer.doIt: " + doer.doIt);
}

class Doer {
int x; // Default 0
int y; // Default 0
boolean doIt = true; // Default true

// Setters
Doer x(int x) {
this.x = x;
return this;
}
Doer y(int y) {
this.y = y;
return this;
}
Doer doIt(boolean doIt) {
this.doIt = doIt;
return this;
}
}


Note that Java allows the same identifier to be used for a data member and method name(s), so "doIt" is separate to "doIt()".

If it is usual to specify x,y values at construction time, it makes sense to specify a constructor that accepts those parameters. You could still do this:

Code:
Doer doer = new Doer(22,33).doIt(false); 



Also note that having multiple method calls is (likely) to have a performance impact compared to a single "dedicated" constructor that does everything you want in one go, so avoid doing this kind of thing in performance-critical code.

-spxl
Re: Class default values
Reply #5 - Feb 26th, 2010, 1:17am
 
Bejoscha wrote on Feb 26th, 2010, 1:10am:
Can somebody explain me the header

myClass (int x, int y, boolean doIt = true) {....}


The code is ACTIONSCRIPT, not Java. It is indicating a default value for a parameter, so that the function/constructor can be called like myClass(x,y) to be the same as myClass(x,y,true).

-spxl
Re: Class default values
Reply #6 - Feb 26th, 2010, 10:10am
 
ah thanks subpixel. that's exactly what i needed. and an interesting way of doing things.
PhilLo's way works. but what i was trying to get at was not having to use them in the constructor, so you only neatly declaring what you need.

Not as neat as ActionScript, but does the job, thanks.
And not that I'm a far of ActionScript - exact opposite, in fact.


cheers
Re: Class default values
Reply #7 - Feb 26th, 2010, 11:00pm
 
Creating multiple constructors isn't so bad. Just make the one with "all the parameters" do all the work, and the simpler ones supply default values.

Code:
class Doer {
 int x; // Default 0
 int y; // Default 0
 boolean doIt; // Default FALSE (but overridden in constructor(s))
 
 // Constructors
 Doer (int x, int y) {
   this(x, y, true);
 }
 Doer (int x, int y, boolean doIt) {
   this.x = x;
   this.y = y;
   this.doIt = doIt;
 }
}


You can even have a (private, if desired) helper method that holds all the common code.

Code:
class Doer {
 int x; // Default 0
 int y; // Default 0
 boolean doIt; // Default FALSE (but overridden in constructor(s))
 
 // Constructors
 Doer (int x, int y) {
   _init(x, y, true);
 }
 Doer (int x, int y, boolean doIt) {
   _init(x, y, doIt);
 }

 // Helper method
 private void _init (int x, int y, boolean doIt) {
   this.x = x;
   this.y = y;
   this.doIt = doIt;
   // and maybe some other tricky stuff here like opening
   // system resources, doing calculations, etc
 }
}


Indeed, having some sort of separate "init" function can be really handy for resetting/reusing an object later constructors can only be called at construction time, an init method can be called any time. Depending on what the init function does, maybe it is okay for it to be public (so callable from outside the class).

Code:
Doer doer = new Doer(22, 33);
// ...
if (someSpecialCondition) doer.init(mouseX, mouseY, true);


-spxl
Page Index Toggle Pages: 1