Introduction
π§ Fundamental Concept: How Java Passes Arguments
Java is strictly pass-by-value.
But here is the important part:
- For primitive types β value is copied
- For objects β reference value is copied
π That means:
When passing an object to a method, Java copies the reference, not the object itself.
This distinction is extremely important π
π’ Part 1: Passing Primitive Data (For Comparison)
Letβs first understand primitives.
βΆοΈ Example 1: Passing Primitive Variable
class Test {
void changeValue(int x) {
x = 50;
}
}
public class PrimitiveDemo {
public static void main(String[] args) {
int a = 10;
Test t = new Test();
t.changeValue(a);
System.out.println(a);
}
}
π¨ Output
10
π§ Explanation
ais 10- Method receives a copy of
a x = 50changes only the copy- Original
aremains unchanged
Primitive values are fully copied.
π’ Part 2: Passing Objects to Methods
Now letβs see what happens with objects.
βΆοΈ Example 2: Passing Object to Method
class Student {
int rollNo;
void changeRoll(Student s) {
s.rollNo = 200;
}
}
public class ObjectPassingDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.rollNo = 100;
s1.changeRoll(s1);
System.out.println(s1.rollNo);
}
}
π¨ Output
200
π§ Step-by-Step Explanation
1οΈβ£ s1 object created in heap
2οΈβ£ s1.rollNo = 100
3οΈβ£ changeRoll(s1) is called
4οΈβ£ Copy of reference is passed
5οΈβ£ Both references point to same object
6οΈβ£ s.rollNo = 200 modifies the object
π Since both references point to the same object, the change is visible outside.
π§ Important Concept: Reference Copy
When passing object:
- Object itself is NOT copied
- Reference is copied
- Both references point to same memory
π’ Part 3: Reassigning Reference Inside Method
Now consider this tricky case.
βΆοΈ Example 3: Reassigning Object Inside Method
class Student {
int rollNo;
void changeObject(Student s) {
s = new Student();
s.rollNo = 500;
}
}
public class ObjectReassignDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.rollNo = 100;
s1.changeObject(s1);
System.out.println(s1.rollNo);
}
}
π¨ Output
100
π§ Why Did It Not Change?
Inside method:
sis a copy of references = new Student()makes it point to new object- Original
s1is unaffected
π Reassigning reference does NOT affect original reference.
π’ Part 4: Returning Objects from Methods
Methods can return objects just like primitive values.
βΆοΈ Example 4: Returning a New Object
class Number {
int value;
Number(int v) {
value = v;
}
Number increment() {
return new Number(value + 1);
}
}
public class ReturnObjectDemo {
public static void main(String[] args) {
Number n1 = new Number(10);
Number n2 = n1.increment();
System.out.println(n1.value);
System.out.println(n2.value);
}
}
π¨ Output
10
11
π§ Explanation
increment()creates new object- Returns new object reference
- Original object remains unchanged
This approach promotes immutability π
π’ Part 5: Returning Current Object (this)
βΆοΈ Example 5: Returning this
class Sample {
int x;
Sample setValue(int x) {
this.x = x;
return this;
}
}
public class ReturnThisDemo {
public static void main(String[] args) {
Sample s = new Sample();
s.setValue(20);
System.out.println(s.x);
}
}
π Returning this enables method chaining.
π’ Part 6: Passing Object and Modifying State
βΆοΈ Example 6: Bank Account Example
class BankAccount {
double balance;
void deposit(double amount) {
balance += amount;
}
}
public class BankDemo {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.balance = 1000;
acc.deposit(500);
System.out.println(acc.balance);
}
}
π¨ Output
1500.0
Object state changes because reference points to same object.
β οΈ Common Confusions
β Is Java pass-by-reference?
β No
β Java is pass-by-value (reference value copied)
β Why object changes but primitive doesn’t?
Because object reference points to same object.
β Can object be cloned automatically?
β No
Must explicitly clone.
β Common Mistakes
β Assuming object is fully copied
Student s2 = s1;
Both refer to same object.
β Reassigning inside method expecting change
s = new Student();
Does not affect original reference.
β Forgetting null checks
void show(Student s) {
System.out.println(s.rollNo); // NullPointerException possible
}
π§ͺ Tricky Example (Very Important)
class Test {
int x = 10;
void modify(Test t) {
t.x = 50;
t = new Test();
t.x = 100;
}
}
public class TrickyDemo {
public static void main(String[] args) {
Test t1 = new Test();
t1.modify(t1);
System.out.println(t1.x);
}
}
π¨ Output
50
π§ Why?
t.x = 50modifies original objectt = new Test()changes only local reference- Original object remains 50
π Key Takeaways
- Java is pass-by-value
- Object reference value is copied
- Modifying object state affects original
- Reassigning reference does not affect original
- Returning objects creates flexibility
- Returning
thisenables chaining
β Conclusion
Passing and returning objects from methods is one of the most important concepts in Java OOP π
By clearly understanding:
- How references work
- How Java passes arguments
- What gets copied
- When objects change
- When they do not change
Java programming becomes predictable, logical, and much easier to reason about πͺ
Mastering this concept lays the foundation for:
- Method chaining
- Immutable design
- Advanced OOP design
- Real-world application architecture