Reputation: 23
i want to creat an array in the main method containing point
like p={(3,8),(2,8)}
with this class
public class Point
{
private float x,y;
public Point(){}
public Point(float abs){this.x=abs;}
public Point(float abs, float ord){this.x=abs; this.y=ord;}
public void setPoint(float abs, float ord){this.x=abs; this.y=ord;}
}
I used p[1] = new Point(5,6);
but when i tried to call p[1].x
i found that x has a private access.
any idea.
Upvotes: 1
Views: 72
Reputation: 17142
You can create an array of Point
s like this:
Point[] points = new Point[] {
new Point(3,8),
new Point(2,8)
};
& in order to be able to access the x
& y
member variables, they should have a public
identifier.
public class Point{
public float x,y;
...
}
But the most optimal way to proceed is by creating a getter:
public class Point{
private float x,y;
...
public float getX() { return x; }
public float getY() { return y; }
}
then you can access the Point
's x
& y
this way :
Point point = new Point(1,1);
point.getX();
point.getY();
Upvotes: 3