Unit 3 Cycle 2 Day 21: Method with Object Parameter
Share
Method with Object Parameter
Section 3.6 — Writing Methods
Key Concept
When a method receives an object as a parameter, it can call that object's methods and modify its state. Since Java passes references by value, the method works with the same object as the caller. Changes to the object's fields through setter calls persist after the method returns. However, reassigning the parameter to a new object does not affect the caller's reference. The AP exam tests this distinction: mutations through the reference are visible, but reassignment of the reference is not.
Consider the following class.
What is printed?Circle big = new Circle(10); Circle small = new Circle(3); System.out.println(big.contains(small) + " " + small.contains(big));
Answer: (B) true false
big.contains(small): 10 > 3 = true. small.contains(big): 3 > 10 = false. Result: "true false".
Why Not the Others?
(A) contains is not symmetric. big contains small, but not vice versa.
(C) big's radius (10) IS greater than small's (3).
(D) big.contains(small) is true because 10 > 3.
Common Mistake
When a method takes an object of the same class as a parameter, this is the calling object and the parameter is the other object. The direction matters.
AP Exam Tip
obj1.method(obj2): this=obj1, parameter=obj2. Swapping which object calls the method can change the result.