In Java, objects are often passed as arguments to methods. This is an important concept in Object-Oriented Programming (OOP) because it helps methods work with real-world data efficiently.
Before understanding why objects are passed as arguments, let us first understand what an object is.
What is an Object in Java?
An object is an instance of a class.
A class is like a blueprint, and an object is the real implementation created from that blueprint.
Example:
class Student {
String name = "Harini";
int mark = 90;
}
Here:
-
Studentis a class -
nameandmarkare data members - An object can be created using the
newkeyword
Student obj = new Student();
Now obj contains the data and behavior of the Student class.
Why Do We Pass Objects as Arguments?
Objects are passed as arguments mainly for the following reasons:
- To access object data inside another method
- To reduce code complexity
- To modify object values
- To achieve code reusability
- To work with real-world applications efficiently
Let us understand each point in detail.
1. To Access Object Data Inside Another Method
When an object is passed to a method, the method can access all the variables and methods of that object.
Example
class Student {
String name = "Harini";
int mark = 90;
}
class Main {
void display(Student s) {
System.out.println("Student Name: " + s.name);
System.out.println("Student Mark: " + s.mark);
}
public static void main(String[] args) {
Student obj = new Student();
Main m = new Main();
m.display(obj);
}
}
Output
Student Name: Harini
Student Mark: 90
Explanation
-
objis an object of theStudentclass - The object is passed to the
display()method - Inside the method,
sreceives the object reference - Using
s.nameands.mark, we access the object's data
This allows methods to work directly with object information.
2. To Reduce Code Complexity
Instead of passing many individual variables, we can pass a single object.
Without Passing Object
display("Harini", 90, "Chennai", 12345);
If the class contains many fields, the method becomes difficult to manage.
With Passing Object
display(studentObj);
The entire data is available through the object.
Benefits
- Cleaner code
- Easy to read
- Easy to maintain
- Reduces parameter list size
3. To Modify Object Values
When an object is passed to a method, the method can modify the object's data.
Example
class Employee {
int salary = 20000;
}
class Main {
void increaseSalary(Employee e) {
e.salary = 30000;
}
public static void main(String[] args) {
Employee emp = new Employee();
Main m = new Main();
System.out.println("Before Increment: " + emp.salary);
m.increaseSalary(emp);
System.out.println("After Increment: " + emp.salary);
}
}
Output
Before Increment: 20000
After Increment: 30000
Explanation
- The original object is passed to the method
- The method changes the
salary - Since both references point to the same object, the original value changes
How Java Passes Objects
This is one of the most important interview concepts.
Java does NOT pass the actual object.
Java passes the reference of the object by value.
What Does "Reference by Value" Mean?
When an object is created:
Student obj = new Student();
-
objstores the memory address (reference) of the object - When passed to a method, Java copies this reference
Example:
display(obj);
Now:
-
objand method parameterspoint to the same object in memory
That is why object data can be modified inside methods.
Memory Representation
obj ---------> Student Object
name = Harini
mark = 90
s ---------> Same Student Object
Both references point to the same object.
Top comments (0)