"nested" constructors?

Hi,

Please does anyone know how to call another constructor of this class from within a constructor, and return the new "this" thus created... oh boy, does that sentence make absolutely zero sense, or what?

For example:

/*immutable*/class Point{

publicfinalint x;

publicfinalint y;

public Point(int x,int y){

this.x = x;

this.y = y;

}

}

/*immutable*/class Line{

publicstaticfinal Point a;

publicstaticfinal Point b;

Line(Point a, Point b){

this.a = a;

this.b = b;

... call methods to calculate line lengths, angles etc etc ...

}

// <<<<<<<<<<<< here's the bit I can't figure out >>>>>>>>>>>

Line(int x1,int y1,int x2,int y2){

returnnew Line(new Point(x1, y1),new Point(x2, y2));

}

... define methods to calculate line lengths, angles etc etc ...

}

Or... Is there an "established convention" for working around this problem? Something like an private initialise() method which actually performs the "guts" of the "master constructor"?

Thanx in advance for any ideas

Keith.

[2422 byte] By [corlettka] at [2007-11-27 7:00:34]
# 1

instead of Line(int x1, int y1, int x2, int y2) {

return new Line(new Point(x1, y1), new Point(x2, y2));

}

use Line(int x1, int y1, int x2, int y2) {

this(new Point(x1, y1), new Point(x2, y2));

}

or you can add some static method like this

pblic static Line getLine(int x1, int y1, int x2, int y2) {

return new Line((new Point(x1, y1), new Point(x2, y2));

}

Message was edited by:

diptaPB

diptaPBa at 2007-7-12 18:51:19 > top of Java-index,Java Essentials,New To Java...
# 2

dipta,

Thank you! ... That's so simple... so apt.

You wouldn't believe how long I've been googling for this... and now that I know the syntax I bet I could find it first time.

Yep... google "java this()" and top of the list is: http://www.fluffycat.com/Java/This-and-Super/

Cheers, and Thanx again.

Keith.

corlettka at 2007-7-12 18:51:19 > top of Java-index,Java Essentials,New To Java...
# 3
just use this(argument) for the demand constructor
eaajea at 2007-7-12 18:51:19 > top of Java-index,Java Essentials,New To Java...