Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Constructor Problems

Started by psychofe69 Oct 12, 2001 at 10:41 PM 5 replies 450+ views
Original Post
psychofe69
psychofe69
class Point { public Point(double x, double y) { this.x = x; this.y = y; } public Point() { Point(0.0, 0.0); } } Ok, I am trying to use the constructor that doesn''t take parameters to call the other constructor as shown above, but I can''t figure out how do it? Could someone PLEASE help me??
davidko
davidko
Usually, it''s called like this:

Point() {
this(0.0, 0.0);
}
Dactylos
Dactylos
[EDIT: Ignore this post, I was incorrect in believing that Java supported default parameters]
In your case you could use default parameters too:
    
class Point
{
public Point(double x = 0.0, double y = 0.0)
{
this.x = x;
this.y = y;
}
}

That way there is no need for the parameter-less constructor, since if a Point is constructed without the 'x' and 'y' arguments, then the default of 0.0 will be used.

i.e.

Point foo = Point(); // foo.x = 0.0; foo.y = 0.0;
Point bar = Point(3.0, 4.0); // bar.x = 3.0; bar.y = 4.0;


Edited by - Dactylos on October 20, 2001 3:10:25 PM
WayfarerX
WayfarerX
*ahem* Java Development forum *ahem* No default parameters
davidko is correct.


"If consquences dictate our course of action, it doesn''t matter what''s right, it''s only wrong if you get caught."
- Tool

"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Kaijin
Kaijin
quote:
Original post by Anonymous Poster
But let me continue by asking why the heck you would want to do this? Why not just build your object the right way by calling the overloaded constructor? What purpose would it be to have an overloaded constructor if you are not going to pass in values to it?

If you are simply using some sort of "smart" initialization, you should use a separate member function (Which WILL work). An overloaded constructor is for YOUR use, not the class.

-nt20



In this example there isn't actually any point in calling the constructor from within the default constructor as you could simply do this:

    
class Point {
double x = 0.0;
double y = 0.0;

public Point() {}

public Point(double x, double y) {
this.x = x;
this.y = y;
}
}


But in general it's quite common to call constructors from inside each other. It's not bad design, the technique is actually used in the Java API itself. The point of overloaded constructors is to give you versatility when creating your objects.

In regard to using a separate method, this is no better than calling another constructor. It can actually lead to hard to find errors if the method is not final. For instance if you have a class A that calls a method init() in it's constructor then you extend that class to create class B. If class B overides init() and the method tries to access fields in B then this will cause problems as when the method is called in the constructor of A (The super classes constructor is called first then sub classes) the extended part of the subclass won't be initialised yet so the fields that init() tries to access won't have been initialised yet either. A hard to spot error if you don't you aren't aware of the order of initialisation during object creation


- Kaijin

"If you find a job that you love you'll never have to work again."

Edited by - Kaijin on October 20, 2001 2:18:53 PM
Dactylos
Dactylos
quote:
Original post by WayfarerX
*ahem* Java Development forum *ahem* No default parameters


Oops, sorry
I actually thought that Java supported default params. As you can probably guess I''m more used to C++. Anyway, thank you WayfarerX for correcting me

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.