AP® Computer Science A glossary
Aliasing
Also called: alias
Course: AP® Computer Science A Shows up in: Unit 3: Class Creation
Two or more references that refer to the same object, so a change made through one reference is seen through the others.
Appears in the course and exam description (CED): 3.6.A.13.6.A.2
Aliases are created by assigning one reference variable to another, by passing an object to a method, and by returning a stored object from a method. To avoid aliasing, make a new object with the same values.
public class Point
{
private int x;
public Point(int x)
{
this.x = x;
}
public static void main(String[] args)
{
Point p = new Point(1);
Point q = p; // q is an alias of p
q.x = 9;
System.out.println(p.x);
}
}
Output:
9 Learn it properly, with a checkpoint 3.6 Methods: Passing and Returning References of an Object