KTU OOP Java2024 Scheme • 60M ESE
MODULE 2/Section 1: Polymorphism

Objects as Parameters, Returning Objects & Java Argument Passing Semantics

HIGH PRIORITY (Prep Guide)Official Syllabus~14 min study
Official KTU 2024 Syllabus Topics Covered Here
Using objects as parametersReturning objects from methods

1. Topic Introduction

Methods don't just exchange int or double values—they routinely accept whole objects as parameters (for example, comparing two objects or constructing a clone) and manufacture new objects as return values. Understanding what happens to object references when passed into a method is essential for code-tracing questions!

2. Start From Zero: Sharing a Google Doc Link vs Photocopying a Number

If you write a number 15 on a sticky note (primitive int) and hand a photocopy to a friend (method(int x)), even if your friend crosses out 15 and writes 99 on their photocopy, YOUR original sticky note stays 15! Now imagine handing your friend a copy of a **Google Doc Edit Link (object reference). Your friend CANNOT change which doc YOUR bookmark points to, but** if they type inside the shared document (obj.a *= 2), you BOTH see the edited text!

Key Takeaway: Java ALWAYS passes arguments strictly by value (call-by-value): primitives pass a copy of their value (caller unchanged), while objects pass a copy of the reference value (mutating fields affects caller's object, reassigning the parameter variable does NOT!).

3. Essential Exam Definitions

Object as Parameter
Passing a class-type reference into a method or constructor (e.g., boolean equalTo(Test o) or copy constructor Box(Box ob)).
Java Argument Passing (Call-by-Value of References)
Java passes a copy of every argument onto the callee's stack frame. For primitives, changes never affect the caller; for objects, the copied reference still points to the caller's Heap object, allowing field mutation (often casually described in older textbooks as behaving like call-by-reference for object state).

4. Detailed Technical Explanation & Syntax

1. Using Objects as Parameters (Equality Check & Copy Constructors)

Two classic uses in Java: (1) comparing two objects (ob1.equalTo(ob2) compares this.a == o.a), and (2) defining a Copy Constructor (Box(Box ob)) that creates a brand-new object initialized identically to an existing object.

2. Clarifying Call-by-Value vs Call-by-Reference (Important Technical Accuracy!)

Some older slides state loosely: 'primitives are passed by value, objects are passed by call-by-reference.' Technically in the Java Language Specification, Java is 100% Call-by-Value! Here is the exact rule for exams:

  • Primitive Argument (void meth(int i)): A copy of the integer value is passed. Mutating i inside meth has ZERO effect on the caller's variable.
  • Object Reference Argument (void meth(Test o)): A copy of the memory reference is passed by value. Therefore: (a) modifying o.a = 50; DOES modify the caller's shared Heap object; (b) reassigning o = new Test(99); only changes the local parameter copy, leaving the caller's reference untouched!

3. Returning Objects from Methods

A method can specify any class name as its return type and return a newly constructed object via return temp;. As long as the caller holds the returned reference, the Heap object stays alive and won't be garbage collected when the method finishes.

5. Working Principle: Stack & Heap Trace When Returning a New Object (incrByTen)

  1. ob2 = ob1.incrByTen(); enters incrByTen() with this pointing to ob1 (a = 2).
  2. Inside incrByTen(), Test temp = new Test(a + 10); creates a new Heap object with a = 12.
  3. return temp; returns the reference of that new object; temp stack frame pops, and ob2 in main holds the object (ob2.a == 12).

7. Complete Java Code Examples & Tracing

Objects as Parameters, Call-by-Value Proof & Returning ObjectsObjectParamReturnDemo.java
Combines equalTo(Test), copy constructor, primitive vs object-reference parameter behavior, and returning a fresh object via incrByTen().
1class ValueBox {
2 int a;
3
4 ValueBox(int a) {
5 this.a = a;
6 }
7
8 // 1. Copy Constructor (Object as parameter)
9 ValueBox(ValueBox other) {
10 this.a = other.a;
11 }
12
13 // 2. Comparing two objects
14 boolean equalTo(ValueBox other) {
15 return this.a == other.a;
16 }
17
18 // 3. Mutating object state vs rebinding reference
19 void modifyStateAndTryRebind(ValueBox param) {
20 param.a += 10; // Affects caller's Heap object!
21 param = new ValueBox(999); // Only rebinds local parameter copy!
22 }
23
24 // 4. Returning a newly created object
25 ValueBox incrByTen() {
26 ValueBox temp = new ValueBox(this.a + 10);
27 return temp;
28 }
29}
30
31public class ObjectParamReturnDemo {
32 public static void main(String[] args) {
33 ValueBox b1 = new ValueBox(2);
34 ValueBox b1Copy = new ValueBox(b1);
35 System.out.println("b1 equals b1Copy? " + b1.equalTo(b1Copy));
36
37 ValueBox b2 = b1.incrByTen();
38 System.out.println("b1.a = " + b1.a + ", b2.a = " + b2.a);
39
40 b1.modifyStateAndTryRebind(b1);
41 System.out.println("After modifyStateAndTryRebind, b1.a = " + b1.a);
42 }
43}
Expected Output
b1 equals b1Copy? true
b1.a = 2, b2.a = 12
After modifyStateAndTryRebind, b1.a = 12
Keywords:classbooleanreturnnewthis
Common Exam & Coding Mistakes
  • Expecting b1.a to become 999 after param = new ValueBox(999);—parameter reassignment never alters the caller's variable in Java.
  • Trying to mutate a primitive int in the caller inside a swap method swap(int x, int y).

Real-World Applications

  • Immutable value classes (String, BigDecimal) return newly constructed objects from every calculation (a.add(b)).
  • Factory methods construct and return customized objects cleanly.

Important KTU Exam Points

  • When asked about Java parameter passing, state clearly: primitives pass copies of values; objects pass copies of their references (so field mutations persist, while reference reassignment does not).
  • Be ready to write incrByTen() (returning an object) and equalTo(Obj o) (object as parameter).

Memory Techniques & Revision

  • Java Parameter Law: 'Mutate Field = Visible; Reassign Parameter = Invisible'.

Common Pitfalls to Avoid

  • Thinking a C++-style swap(int a, int b) works in Java—because Java is strictly call-by-value, swapping local parameters leaves caller ints unchanged.

KTU-Style Questions & Exam-Ready Answers

Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.

Introduction: Java methods can accept class-type objects as arguments and return newly created or processed objects as return values.

  • Objects as Parameters: Receiving an object reference (Test ob) passes a copy of the reference by value, letting the method read or mutate the object's fields (equalTo(Test), copy constructors).
  • Returning Objects: A method specifies a class return type (Test incrByTen()) and returns an object reference created on the Heap, which remains valid after the method returns.
class Test {
int a;
Test(int i) { a = i; }
Test incrByTen() { return new Test(a + 10); }
}

Quick Understanding Check

1. Inside void reset(Box b) { b.w = 5; b = new Box(99); }, if caller passes Box x = new Box(1);, what is x.w afterwards?