Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two ways to get value of Point object?

Tags:

java

point

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?

like image 204
user2097804 Avatar asked Jun 13 '13 20:06

user2097804


2 Answers

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.

like image 52
Daniel Kaplan Avatar answered Sep 23 '22 07:09

Daniel Kaplan


Change to

 double x0 = p.getX();

 // getX returns the X coordinate of this Point2D in double precision
like image 27
DVK Avatar answered Sep 22 '22 07:09

DVK