How come you can get the x and y values from a java.awt.Point class by using a method and referencing the value?
Point p = new Point(10,20);
int x0 = p.getX();
int y0 = p.getY();
int x1 = p.x;
int y1 = p.y;
System.out.println(x0+"=="+x1+"and"+y0+"=="+y1);
Did the people who made this class forget to make x and y private?
Looking at the javadoc, these seem to return different types. p.x
returns an int
while p.getX()
returns a double
.
The source code of Point
shows this:
public int x;
//...
public double getX() {
return x;
}
So it looks like that's its only purpose. getX()
is a more convenient way to get the coordinates as a double
.
Change to
double x0 = p.getX();
// getX returns the X coordinate of this Point2D in double precision
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With