Passing Objects to Methods in Java
1. Passing Objects as Parameters
Objects can be passed to methods as parameters just like primitive values. In Java, when an object is passed, a reference to it is passed by value. (Strictly speaking, everything in Java is passed by value. However, for the sake of simplicity, we will consider that objects are passed by reference.) This allows the method to modify the state of the received object. The most important question is: will these changes persist in the calling method? If an object is passed, yes; if it is a primitive value, no.
Example 1: Modifying Values in a Method
When a program runs, a memory area in the stack is allocated for the main() method, storing local variables like x and y. When changePrimitives() is called, a new stack frame is created with its own local variables. Changes to these variables do not affect the original x and y because they reside in different memory blocks.
However, when an object is passed to changeObject(), the reference is copied, but both references point to the same object in the heap. Therefore, modifications to the object's fields inside the method are reflected globally:
public class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
} public class TestDemo {
/**
* Change an object
*
* @param box
*/
static void changeObject(Box box) {
box.width *= 2;
box.height /= 2;
box.depth += 2;
}
/**
* Change primitive type
*
* @param a
* @param b
*/
static void changePrimitives(int a, int b) {
a *= 2;
b /= 2;
}
public static void main(String[] args) {
Box box = new Box(5, 6, 7);
int x = 10;
int y = 10;
System.out.println("x and y before method invocation: " + x + " " + y);
changePrimitives(x, y);
System.out.println("x and y after method invocation: " + x + " " + y);
System.out.println("box before method invocation: " + box.width + " " + box.height + " " + box.depth);
changeObject(box);
System.out.println("box after method invocation: " + box.width + " " + box.height + " " + box.depth);
}
} Result of program:
x and y before method invocation: 10 10
x and y after method invocation: 10 10
box before method invocation: 5.0 6.0 7.0
box after method invocation: 10.0 3.0 9.0 2. Returning Objects from Methods
Methods can also create and return new objects. This is useful when you want a method to produce a modified version of an object without changing the original one.
public class ReturnObjectExample {
int a;
ReturnObjectExample(int i) {
a = i;
}
ReturnObjectExample incrementByTen() {
ReturnObjectExample temp = new ReturnObjectExample(a + 10);
return temp;
}
public static void main(String[] args) {
ReturnObjectExample ob1 = new ReturnObjectExample(2);
ReturnObjectExample ob2 = ob1.incrementByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
}
} Conclusion: Understanding that Java passes the value of the reference is key to avoiding bugs when working with mutable objects.
Please log in or register to have a possibility to add comment.