0% found this document useful (0 votes)
7 views3 pages

Omooop 9

The document discusses the concept of passing objects as arguments in Java, highlighting the difference between passing primitive types and objects. It explains that objects are passed by reference, allowing methods to modify the original object. An example program is provided, demonstrating the creation of an Employee class, methods for displaying and updating employee details, and returning the employee object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Omooop 9

The document discusses the concept of passing objects as arguments in Java, highlighting the difference between passing primitive types and objects. It explains that objects are passed by reference, allowing methods to modify the original object. An example program is provided, demonstrating the creation of an Employee class, methods for displaying and updating employee details, and returning the employee object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 9

Program on passing object as argument and returning object

Description:
When we pass a primitive type to a method, it is passed by value. But when we pass an object to a method,
the situation changes dramatically, because objects are passed by what is effectively call-by-reference.
Java does this interesting thing that’s sort of a hybrid between pass-by-value and pass-by-reference.
Basically, a parameter cannot be changed by the function, but the function can ask the parameter to change
itself via calling some method within it.
● While creating a variable of a class type, we only create a reference to an object. Thus, when we pass
this reference to a method, the parameter that receives it will refer to the same object as that referred to
by the argument.
● This effectively means that objects act as if they are passed to methods by use of call-by-reference.
● Changes to the object inside the method do reflect in the object used as an argument.

CONCLUSION: Students are expected to List the error they are facing along with their solution

REFERENCES : www.learnjavaonline.org/ www.javaworld.com www.w3schools.com


www.codejava.net/books/4-best-free-java-e-books-for-beginners

Program on passing object as argument and returning object

public class Employee {


private String name;
private int age;
private double salary;

// Constructor to initialize employee details


public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}

// Method to display employee details


public void displayDetails() {
System.out.println("Om Gaikwad");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}

// Method to return the employee object


public Employee getEmployee() {
return this;
}

// Method to update employee details


public void updateDetails(Employee emp, String name, int age, double salary) {
emp.name = name;
emp.age = age;
emp.salary = salary;
}

public static void main(String[] args) {


// Create an employee object
Employee emp = new Employee("Om Gaikwad", 30, 50000.0);

// Display employee details


System.out.println("Original Employee Details:");
emp.displayDetails();

// Pass the employee object to the updateDetails method


emp.updateDetails(emp, "Om Gaikwad", 35, 60000.0);

// Display updated employee details


System.out.println("\nUpdated Employee Details:");
emp.displayDetails();

// Return the employee object from the getEmployee method


Employee returnedEmp = emp.getEmployee();
// Display returned employee details
System.out.println("\nReturned Employee Details:");
returnedEmp.displayDetails();
}
}}
Output:

You might also like