Lesson 3.6: Methods: Passing and Returning Object References
Lesson 3.6: Methods: Passing and Returning Object References
What You'll Learn
- Explain how object references are passed to methods
- Distinguish mutating through a parameter vs reassigning the parameter reference
- Identify aliasing and predict its effects
- Apply EK 3.6.A.3: same-class access to private fields
- Explain what is returned when a method returns an object reference
Key Vocabulary
| Term | Definition |
|---|---|
| object reference | Variable storing the memory address of an object, not the object itself |
| alias | Two reference variables pointing to the SAME object in memory |
| mutable object | Object whose state can be changed after creation (has setters) |
| immutable object | Object whose state cannot be changed after creation (e.g., String in Java) |
| same-class access | A method can access private data of a parameter if the parameter is the SAME type as the enclosing class (EK 3.6.A.3) |
| null | A reference that points to no object; calling a method on null causes NullPointerException |
CED EK 3.6.A.1–3.6.A.3
When an object reference is passed, the parameter receives a COPY of the reference -- both the caller and parameter point to the SAME object (EK 3.6.A.1). A method CAN change a mutable object's state. When a method returns an object reference, the caller receives a reference to the SAME object (EK 3.6.A.2). A method CANNOT access private data of a parameter unless the parameter is the same type as the enclosing class (EK 3.6.A.3).
How Object References Are Passed
Mutating a Passed Object
public class Baker {
public void fillJar(Jar j) {
j.addCookies(10); // changes the SAME jar
}
}
Jar myJar = new Jar(5);
new Baker().fillJar(myJar);
System.out.println(myJar.getCookies()); // 15
j is a copy of the reference -- points to the SAME jar. Calling j.addCookies(10) mutates the shared object.
Aliasing
Two Variables, One Object
Jar jar1 = new Jar(5); Jar jar2 = jar1; // alias! jar2.addCookies(10); System.out.println(jar1.getCookies()); // 15, not 5
jar2 = jar1 makes both point to the same Jar. Changes through jar2 are visible through jar1.
AP Trap: Object Parameter vs Primitive Parameter
Primitives: copy of value -- caller unaffected. Objects: copy of reference -- SAME object is reachable; state CAN be changed.
AP Trap: Reassigning Parameter Reference Doesn't Affect Caller
public void replaceJar(Jar j) {
j = new Jar(100); // replaces local copy only
}
Jar myJar = new Jar(5);
replaceJar(myJar);
// myJar still has 5 cookies
Reassigning j to a new Jar replaces the LOCAL reference copy. myJar in the caller is unaffected. This is DIFFERENT from calling a mutator method on j.
AP Trap: Cannot Access Another Class's Private Data
public class Baker {
public void inspect(Jar j) {
System.out.println(j.cookies); // COMPILE ERROR
}
}
EK 3.6.A.3: Baker != Jar. Baker cannot access Jar's private fields directly.
Same-Class Private Access (EK 3.6.A.3)
Valid Same-Class Access
public class Point {
private int x; private int y;
public boolean equals(Point other) {
return this.x == other.x && this.y == other.y;
// other.x is valid: other is also a Point
}
}
A method inside Point CAN access other.x and other.y because other is also a Point.
Real-World Connection: Passing a BankAccount to a transfer() method lets it call account.withdraw() -- mutating the same account. Powerful but requires careful design.
Summary
- When an object reference is passed, the parameter is a COPY of the reference; both point to the SAME object (EK 3.6.A.1).
- A method CAN change a mutable object passed as a parameter by calling its mutators.
- Reassigning the parameter reference does NOT affect the caller's variable.
- Returned reference points to the SAME object (EK 3.6.A.2).
- Same-class: method can access private fields of a same-type parameter. Different class: compile error (EK 3.6.A.3).
Practice Questions
change(myBox) where change calls b.setVal(99) and myBox=Box(1), result?Box a = new Box(5); Box b = a; b.setVal(20); System.out.println(a.getVal());
public static void reset(Box b) { b = new Box(0); } called with Box(42). What does myBox.getVal() return?other.width where width is private and other is Rectangle parameter. TRUE?w1.transfer(w2, 30) does money -= amt; other.money += amt;. Output?Dog winner = competition.getBest();, calling winner.setName("Rex");...Mastery: Methods
// doubleNode: n.setData(n.getData() * 2) new Graph().doubleNode(myNode); // myNode started as Node(7) System.out.println(myNode.getData());
a.getData() print?Node a = new Node(10); Node b = a; b.setData(50); b = new Node(99);
public void swap(Node a, Node b) reassigns a and b. Do caller variables change?public boolean sameAs(Node other) { return this.data == other.data; } -- data is private. Compiles?Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]