By LearnAllStudents Editorial Team. Source check dates are listed below.
Follow the worked example
Suppose Box has an integer count field. Box a = new Box(3); Box b = a; After b.setCount(9), a.getCount() returns 9.
The assignment copies the reference, so one object changed. It did not create a second Box.
Now let b = new Box(2). The new assignment changes what b refers to. a still identifies the earlier Box with count 9.
Place the listing inside a Java class. Try the worked example’s inputs from its main method.
static final class Box {
private int count;
Box(int count) {
this.count = count;
}
void setCount(int count) {
this.count = count;
}
int getCount() {
return count;
}
}
static int[] traceBoxCounts() {
Box a = new Box(3);
Box b = a;
b.setCount(9);
int afterMutation = a.getCount();
b = new Box(2);
return new int[] { afterMutation, a.getCount(), b.getCount() };
}Draw objects separately from variables
Draw a box for each object created with new and an arrow from each variable that refers to it. Assigning one reference to another copies an arrow. A setter changes the box. Reassigning a variable moves only its arrow.
Try it yourself
After c = a, call c.setCount(5). Then set c = null. What does a.getCount() return?
Check your reasoning
5. The method changed the first Box before c stopped referring to it.
Use the idea in a different situation
What change to the original code would give b independent state at construction?
Check the second task
Use a new Box with a starting value, rather than assigning a’s reference directly.
For parents
Ask, “When b is set to a new Box, what happens to the first Box?”
They should keep the first box and its value, then move only b’s arrow to the new one.
Use paper arrows for the variables. Move arrows for assignments and change values inside boxes for setter calls.
How a tutor would approach this
Write down the expected output or state before running the Java code. Compare it with the actual output, trace where they first differ and test the fix against another input that arrives at the same kind of boundary.
Frequently asked questions
What mistake should I look for first?
Reassigning a reference changes the variable. Calling a method that mutates the object changes every view of that same object.
How can I use this with a longer practice set?
Read the current course framework and official sample questions before deciding how to practise this technique in an exam-length task.
Are these official past-paper questions?
The worked example and practice tasks here are original. The sources section links to the official material for your course.
How can I tell if I can use the method on a new question?
Try the second task with the worked example covered. Explain which step needs to change and why before checking the answer.
Official sources
- College Board AP Computer Science A course and exam description (checked 2026-11-26)
Related pages
Try a free 30-minute demo
Work through a question with a tutor. No payment needed.
Request a free 30-minute demo