From the free course AP Computer Science A on Lykke
3.6 Methods: Passing and Returning References of an Object
Key concepts: Java uses pass-by-value for method arguments · Object references are values that can be copied when passed to methods · A copied reference can still refer to the same mutable object · Methods can modify the state of a mutable object through a reference parameter · Reassigning a reference parameter does not change the caller’s reference · Returning an object reference can give the caller access to the existing object rather than a new copy · A reference variable and the object it refers to are distinct · A reference may hold null · Private data and methods of a referenced parameter cannot be accessed directly outside its class · Enhanced for-loop variables do not change the corresponding values in the collection when reassigned
Java passes every method argument by value. For an object, that value is not the entire object—it is a reference, a value that identifies where the object can be found.
3.6 Methods: Passing and Returning References of an Object
Java passes every method argument by value. For an object, that value is not the entire object—it is a reference, a value that identifies where the object can be found. The result is a subtle but powerful rule: a method can change an object through a copied reference, but it cannot replace the caller’s reference variable.
Essential question: If Java copies a reference when calling a method, how can the method change the caller’s object—but not the caller’s variable?
Two things that must not be confused
A reference variable stores a connection to an object. The object stores the actual attributes and behavior. Copying a reference creates a second connection to the same object; it does not create a second object.
Consider a mutable Badge object. “Mutable” means that its state—the values of its attributes—can change after construction.
Badge visitor = new Badge("Visitor");
changeLabel(visitor);
During the call, Java initializes the parameter with a copy of visitor’s reference:
public static void changeLabel(Badge badge) {
badge.setLabel("Staff");
}
Both visitor and badge refer to the same Badge object. Therefore, setLabel changes the object’s state, and the caller observes the change:
System.out.println(visitor.getLabel()); // Staff
This is the central rule in 3.6.A.1: when an argument is an object reference, the parameter receives a copy of that reference, not an independent copy of the object. If the object is mutable, the method can alter its state through the parameter. Good programming practice is to modify a mutable parameter only when the specification requires it.
Mutation is not reassignment
Changing the object and changing the parameter variable are different operations. Calling a method such as badge.setLabel(...) modifies the shared object. Assigning badge = ... changes only the local parameter.
public static void replaceBadge(Badge badge) {
badge = new Badge("Temporary");
}
After replaceBadge(visitor), visitor still refers to the original object. The parameter was reassigned, but the caller’s reference variable was untouched.
The same result occurs when the parameter is assigned null:
public static void discard(Badge badge) {
badge = null;
}
discard(visitor) does not make visitor equal to null; it only makes the local copy of the reference equal to null.
Named misconception — “Java passes objects by reference.”
Java always passes arguments by value. For objects, the copied value happens to be a reference. That copied reference can reach the same mutable object, which is why mutation is visible.
Returning an object reference
A method that returns an object returns a reference to an object. It does not automatically create a copy.
public static Badge getBadge(Badge badge) {
return badge;
}
Badge secondName = getBadge(visitor);
After this call, secondName and visitor refer to the same Badge object. Mutating through either reference affects that one object. A new object exists only if the method explicitly constructs one, such as return new Badge("Visitor");.
null and access control
null means that a reference points to no object. Calling an instance method through a null reference causes a NullPointerException; the reference must identify an actual object first.
Access is also limited by the class’s access control. Code outside a class cannot directly access that class’s private attributes or methods through a reference. It must use accessible methods, such as public getters or setters. 3.6.A.3 specifies that a method cannot access the private data or methods of a parameter’s object unless the parameter is the same type as the method’s enclosing class.
Enhanced for loops copy values
An enhanced for loop variable also receives a copied value. With object references, the copied value still refers to the same object:
for (Badge badge : badges) {
badge.setLabel("Checked");
}
This can mutate every referenced Badge. However, assigning a new object to the loop variable does not replace the collection element:
for (Badge badge : badges) {
badge = new Badge("Replacement");
}
The collection remains unchanged because only the temporary loop variable was reassigned. To replace elements, use indexed assignment or an appropriate collection method.
Skill connection and retrieval check
This topic develops 3.6.A Develop code to define behaviors of objects and relies on 3.D Explain why a code segment will not compile or work as intended and modify the code to correct the error. On an exam, trace the reference and object separately before predicting output.
Retrieval check: A method receives Badge b, executes b = null, and returns. The caller’s variable originally referred to a valid badge. What does the caller’s variable refer to afterward—and what single change would make the caller’s reference become null?
Answer: It still refers to the original badge because the parameter was only a copied reference. The caller must assign the returned value, for example b = discardAndReturnNull(b);, or assign b = null directly.



Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A
- Bullet What data can be provided to students to ensure their programs are thoroughly tested?
- The method call moveCandyToFirstRow(0) returns false because the box element at row 0 and column 0 does not contain a piece of candy and there are no pieces of candy in column 0 that can be moved to r