user2097804
user2097804

Reputation: 1132

Two ways to get value of Point object?

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?

Upvotes: 5

Views: 11124

Answers (2)

DVK
DVK

Reputation: 129481

Change to

 double x0 = p.getX();

 // getX returns the X coordinate of this Point2D in double precision

Upvotes: 1

Daniel Kaplan
Daniel Kaplan

Reputation: 67440

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.

Upvotes: 5

Related Questions