I have a Rectangle
class and its constructor sets every variable (x, y, width, height) to a specific value. After a Rectangle
is being created, if I want to change all of its values. To do that, is it more efficient to have a function like rect.set(newX, newY, newWidth, newHeight);
or to call the constructor r1 = new Rectangle(newX, newY, newWidth, newHeight);
all over again ? (Since I won't be referencing the older Rectangle
anymore)
public Rectangle (int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void set (int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
I would imagine that creating a new rectangle will create garbage, so it should be worse. Is this true ? or does java somehow optimize this ?