whirlwin
whirlwin

Reputation: 16521

Why does Point allow you to access x and y directly?

Both x and y and integers, while getX() and getY() returns a doubles.

Why aren't there e.g. getXIntValue(), getXDoubleValue() and vice versa for y?

Upvotes: 4

Views: 118

Answers (3)

Luigi Plinge
Luigi Plinge

Reputation: 51109

As others have said, it's because it's ancient. But I like it because it saves typing and adds clarity; IMHO getters and setters can be a waste of time for simple classes if they're public and all they're going to do is to return the value and set it, which is all you want 99% of the time.

Design philosophy changed and now everything uses them. But things are coming full circle and in a modern languages like Groovy these would be "properties" with getters / setters inferred and syntax pretty similar to that for accessing public fields in Java.

E.g. http://groovy.codehaus.org/Groovy+Beans

Edit: and according to an (unofficial) Scala style guide,

Note that fields may actually be used in a number of situations where accessors and mutators would be required in languages like Java. Always prefer fields over methods when given the choice.

Upvotes: 1

Sai
Sai

Reputation: 3957

Point extends Point2D and Point2D has getX() and getY() defined as returning double. As @Dilum mentions above, its bad design from the stone ages of java.

Upvotes: 4

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

Note the @since in the Javadoc -- 1.0

There is broad consensus that Point was very poorly designed.

Upvotes: 5

Related Questions