DEV Community

Harini
Harini

Posted on

Why Objects Are Passed as Arguments in Java – Complete Guide for Beginners

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;
}
Enter fullscreen mode Exit fullscreen mode

Here:

  • Student is a class
  • name and mark are data members
  • An object can be created using the new keyword
Student obj = new Student();
Enter fullscreen mode Exit fullscreen mode

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:

  1. To access object data inside another method
  2. To reduce code complexity
  3. To modify object values
  4. To achieve code reusability
  5. 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Student Name: Harini
Student Mark: 90
Enter fullscreen mode Exit fullscreen mode

Explanation

  • obj is an object of the Student class
  • The object is passed to the display() method
  • Inside the method, s receives the object reference
  • Using s.name and s.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);
Enter fullscreen mode Exit fullscreen mode

If the class contains many fields, the method becomes difficult to manage.

With Passing Object

display(studentObj);
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Before Increment: 20000
After Increment: 30000
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode
  • obj stores the memory address (reference) of the object
  • When passed to a method, Java copies this reference

Example:

display(obj);
Enter fullscreen mode Exit fullscreen mode

Now:

  • obj and method parameter s point 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
Enter fullscreen mode Exit fullscreen mode

Both references point to the same object.


Top comments (0)