Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing an object without new operator in java

Tags:

java

Whats the difference between these two constructors.

Circle (Point2D center, double radius) {
    this.center = center;
    this.radius = radius;
}

Circle (Point2D center, double radius) {
    this.center = new Point2D(center.getX(), center.getY());
    this.radius = radius;
}

Heres the Point2D class. Both versions of the constructors are working just fine. I am just confused whats the difference.

class Point2D {
double x;
double y;

Point2D () {
    x = 0;
    y = 0;
}

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

void setX (double x) {
    this.x = x;
}

void setY (double y) {
    this.y = y;
}

void setXY (double x, double y) {
    this.x = x;
    this.y = y;
}

double getX () {
    return x;
}

double getY () {
    return y;
}

double distance (Point2D p) {
    double x1 = p.getX();
    double y1 = p.getY();
    return Math.sqrt(Math.pow(x1 - x, 2) + Math.pow(y1 - y, 2));
}

double distance (double x, double y) {
    return Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2));
}

}

Also the Circle class has 2 fields center and radius. center is type Point2D and radius is type double. I tried compiling and running both versions of the constructor. Both works well. So i am confused which one will be better to use and why, also the difference because in first one i havent used the new operation to initialize the center object.

like image 251
Xtr33mm Avatar asked Mar 12 '23 18:03

Xtr33mm


2 Answers

second constructor uses defensive copy (witch is produced by Point2D constructor call) of the original center instance to prevent further mutation (changes) of the input instance that may be very confusing and produce bugs

So i am confused which one will be better to use and why

the second approach is more safe because your Point2D class is mutable. It uses more memory (to hold additional defensive copy) but in real complex projects there are more pros for this design practice than cons.

If you can make Point2D class immutable then it will be ok to use the first constructor.

like image 97
Cootri Avatar answered Mar 15 '23 07:03

Cootri


The second constructor creates a new Point instance - this.center = new Point2D(center.getX(), center.getY()); instead of keeping a reference to the instance passed to it. This means that if the passed center is later modified, the changes doesn't affect the Circle instance.

The second constructor is usually safer, since it has a truly private Point instance as the center of the Circle, while the first constructor shares the center with the caller of the constructor.

You can see the difference with this code :

Point2D point = new Point2D(10.0, 20.0);
Circle circle = new Circle (point, 4.0);
point.setX (5.0);
System.out.println(circle); // assuming you override toString to display the properties
                            // of the Circle

If you use the first constructor, after this code is executed, your circle will have a center of (5.0,20.0), while if you use the second constructor, the circle will have a center of (10.0,20.0);

like image 37
Eran Avatar answered Mar 15 '23 06:03

Eran