Evorlor
Evorlor

Reputation: 7569

Should I use Point.x or Point.getX()?

I have a Point. I am trying to get x as an int. If I use Point.x, I will get x as an int. But I am under the impression that I should be using a getter whenever possible (Why use getters and setters?). The issue with Point.getX() is that it returns a double instead of an int.

Which is better, or is it just preference?

a or b?

Point point = new Point(5, 5);
int a = point.x;
int b = (int) point.getX();

I have read Java Point, difference between getX() and point.x, but it did not really answer my question. Or at least I did not understand the answer.

Upvotes: 7

Views: 11194

Answers (4)

Marco13
Marco13

Reputation: 54659

As already pointed out in other answers: The inheritance hierarchy of Point2D is ... somewhat unusual. The reason for that is simple:

From the documentation of Point:

A point representing a location in (x,y) coordinate space, specified in integer precision.

Since: 1.0

From the documentation of Point2D:

The Point2D class defines a point representing a location in (x,y) coordinate space.

Since: 1.2

Simply put: The Point class existed since the beginning of time, and the Point2D class was introduced later. The Point class has then been "retrofitted" to also be a Point2D, and offer the getX() methods, for example.


When you are working with Point, and dedicatedly only with Point, and you always and only want to deal with int coordinates, then you can use point.x to access the field directly.

Otherwise, when double coordinates make sense for your application case, and you want, for example, a method that receives a "point" as a parameter, you usually should consider using the abstract Point2D class, because then you can pass a Point, a Point2D.Double or a Point2D.Float instance to this method.

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42174

The getX() and getY() functions of the Point class return a double because that's what its parent class (Point2D) requires. This is so all of its subclasses (Point2D.Double and Point2D.Float) will all work in the same places.

Using point.x and point.y directly instead of using the getter functions is probably fine in this case, since the Point class is a weird corner case that nobody seems to love- most people think it should be immutable, or at least better hide its values.

If you're afraid of something like a code review, just throw a comment in there explaining why you're accessing the x and y variables directly.

tl;dr: it just comes down to personal preference.

Upvotes: 4

Saif
Saif

Reputation: 7052

As the double return type of the getter methods of point class is only to make it compatible through the inheritance and the getX() do nothing without just returning value.

SO, Its just about your preference, I guess.
But like use point.x (why making the unnecessary conversion? ) in this case only, Although I prefer using getter/setter always.

Upvotes: 1

JP Moresmau
JP Moresmau

Reputation: 7403

I think, in short, that the designers of this class didn't want to create another couple of functions getXAsInteger and getYAsInteger, but still give you the option to get the underlying integer values, instead of the values converted to double. So make an exception in that case and use the fields directly.

Upvotes: 1

Related Questions