Lesson 3.6: Methods: Passing and Returning Object References

Unit 3 · Lesson 3.6 · Code Mechanics

Lesson 3.6: Methods: Passing and Returning Object References

🕑 35–40 min· 8 Practice Questions· 4 Mastery Questions· Output Predictor + Bug Hunt

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).
Tier 2 · AP Practice

Practice Questions

MCQ 1
After change(myBox) where change calls b.setVal(99) and myBox=Box(1), result?
⚠ Predict the answer before reading the options.
A 1
B 0
C Compile error
D 99
D is correct. b is a copy of reference to same Box. setVal(99) mutates shared object.
MCQ 2
What is printed?
Box a = new Box(5);
Box b = a;
b.setVal(20);
System.out.println(a.getVal());
A 5
B 20
C 0
D Compile error
B is correct. b=a is an alias. b.setVal(20) changes shared object. a.getVal()=20.
MCQ 3
public static void reset(Box b) { b = new Box(0); } called with Box(42). What does myBox.getVal() return?
⚠ Predict the answer before reading the options.
A 0
B 42
C NullPointerException
D Compile error
B is correct. Reassigning b (local ref copy) does NOT affect myBox. Still Box(42).
MCQ 4
Inside a Rectangle method, other.width where width is private and other is Rectangle parameter. TRUE?
A Compile error
B Valid: same-class access to same-type parameter private field (EK 3.6.A.3)
C Valid only if width is static
D Valid only if other is public
B is correct. EK 3.6.A.3: same-class access is permitted.
MCQ 5
w1 starts at 100, w2 at 50. w1.transfer(w2, 30) does money -= amt; other.money += amt;. Output?
A 100 50
B 70 80
C 130 20
D Compile error
B is correct. w1=70, w2=80. Same-class private access is valid (EK 3.6.A.3).
MCQ 6
Which statement about EK 3.6.A.3 is TRUE?
A A method can access private fields of ANY object parameter
B A method can access private fields of a parameter ONLY when the parameter is the same type as the enclosing class
C Private fields can never be accessed
D Private fields become public when passed as parameters
B is correct. Same-type = allowed; different type = compile error.
MCQ 7
Which CORRECTLY distinguishes primitive vs object reference passing?
A Both pass exact copies; neither affects the original
B Primitives: copy of value; Objects: copy of reference to same object; methods CAN mutate the object
C Objects pass a copy of the entire object
D Primitives pass the original variable
B is correct. EK 3.5.A.8 (primitives) + EK 3.6.A.1 (objects).
MCQ 8
After Dog winner = competition.getBest();, calling winner.setName("Rex");...
A Creates a new Dog; competition's dog unchanged
B Throws NullPointerException
C Changes the same Dog object the competition holds
D Compile error
C is correct. EK 3.6.A.2: returned reference points to same object.
PRACTICE WITH A GAME — CHOOSE ONE:
Output Predictor: Object References
Predict carefully -- aliasing and reference copies.
Question 1 of 3  ·  Score: 0

Done!
You got 0 of 3 correct.
Bug Hunt: Object References
Click the buggy line, or 'No bug'.
Question 1 of 3  ·  Score: 0
No bug — code is correct
Done!
You got 0 of 3 correct.
Tier 3 · AP Mastery

Mastery: Methods

MCQ 1
What is printed?
// doubleNode: n.setData(n.getData() * 2)
new Graph().doubleNode(myNode);  // myNode started as Node(7)
System.out.println(myNode.getData());
A 14
B 7
C 0
D Compile error
A is correct. doubleNode receives ref copy. setData(14) mutates shared Node.
MCQ 2
After this code, what does a.getData() print?
Node a = new Node(10);
Node b = a;
b.setData(50);
b = new Node(99);
⚠ Predict the answer before reading the options.
A 99
B 10
C 0
D 50
D is correct. b=a alias. b.setData(50) changes shared node to 50. b=new Node(99) moves b; a still has 50.
MCQ 3
public void swap(Node a, Node b) reassigns a and b. Do caller variables change?
A Yes: object references modified by called methods
B Yes, if nodes are mutable
C No: reassigning parameters changes only local copies
D No: Node is immutable
C is correct. Reassigning local reference copies does not affect caller.
MCQ 4
public boolean sameAs(Node other) { return this.data == other.data; } -- data is private. Compiles?
A No: private inaccessible
B No: == can't compare private
C Yes, but only if data is static
D Yes: same-class access (EK 3.6.A.3)
D is correct. Inside Node, private fields of a Node parameter are accessible.

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]