I have a LINE class that has two attributes of type POINT (which is an object).
public class LINE {
private Point p1,p2;
}
If I make this statement, will it cause aliasing?
public void setP1(Point p1)
{
this.p1=p1;
}
Or Do I have to create a new object, like that:
public void setP1(Point p1)
{
this.p1=new Object(p1);
}
Thanks in advnace.
Yes, it will cause aliasing.
My guess is that you're trying to create a copy of the object, than you want to instantiate a new Point
.
That being said, if you Don't actually need to make a copy of the object, then the first one will do
so if you call it like
Line line = new Line()
line.setP1(new Point(/*params*/))
that's perfectly okay.