문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top