pass objects as an arguments in java
Yes, in Java, you can pass objects as arguments to methods. When you pass an object to a method, you are passing the reference to that object. This allows the method to modify the object’s attributes or call its methods, as the reference points to the same object in memory.
Table of Contents
Example of Passing Objects as Arguments
Consider a class `Person` with some attributes and a method that modifies one of those attributes:
```java
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class PassObjectDemo {
public static void main(String[] args) {
Person person = new Person("John", 30);
System.out.println("Before modification:");
person.display();
modifyPerson(person);
System.out.println("After modification:");
person.display();
}
static void modifyPerson(Person p) {
p.age = 35; // Modifying the age attribute of the person object
}
}
```
Explanation:
- 1. Class Definition: The `Person` class has two attributes (`name` and `age`) and a method `display()` to print these attributes.
- 2. Object Creation: In the `main` method, an instance of `Person` is created with the name “John” and age 30.
- 3. Before Modification: The `display` method is called to print the initial state of the `person` object.
- 4. Passing Object to Method: The `person` object is passed to the `modifyPerson` method. Inside this method, the `age` attribute of the `person` object is modified.
- 5. After Modification: The `display` method is called again to print the modified state of the `person` object.
When the `modifyPerson` method is called, it receives the reference to the `person` object. Any changes made to the object inside the method affect the original object.
Key Points
- 1. Pass by Reference (Effectively): In Java, object references are passed by value, but since the value is a reference to the object, the effect is similar to pass-by-reference found in other languages. The method receives a copy of the reference, which points to the same object.
- 2. Modifying Objects: Any modifications made to the object inside the method are reflected in the original object, as both the original reference and the copied reference point to the same memory location.
- 3. Immutable Objects: If the object being passed is immutable (e.g., instances of the `String` class), modifications to the object’s content are not possible. However, you can still reassign the reference within the method.
```java
public class PassStringDemo {
public static void main(String[] args) {
String greeting = "Hello";
System.out.println("Before modification: " + greeting);
modifyString(greeting);
System.out.println("After modification: " + greeting);
}
static void modifyString(String str) {
str = "Goodbye"; // This modifies the local reference, not the original object
}
}
```
Explanation of Immutable Example
- 1. String Creation: A `String` object `greeting` is created with the value “Hello”.
- 2. Before Modification: The initial value of `greeting` is printed.
- 3. Passing Immutable Object: The `greeting` object is passed to the `modifyString` method. Inside this method, the local reference `str` is reassigned to a new string “Goodbye”.
- 4. After Modification: The value of `greeting` remains unchanged outside the method because the reassignment of the local reference `str` does not affect the original `greeting` reference.
In summary, passing objects as arguments in Java is straightforward and follows the principle of passing references by value. Changes to the object within the method will affect the original object unless the object is immutable or the reference itself is reassigned within the method.