Skip to content

Is Java pass by Reference?

kgleong edited this page Sep 10, 2015 · 1 revision

Java actually passes all parameters by value.

This is evident by looking at the example below:

// Creates a pointer to a person object called person1.
Person person1;

// Saves the address for the new person object in person1.
person1 = new Person();
person1.firstName = "John";

// Passes in the address to the new person object,
// but does not change the address saved in person1.
switchToNewPerson(person1);

System.out.println(person1.firstName) // Prints 'John'

void switchToNewPerson(Person person) {
    person = new Person();
    person.firstName = "Jane";
}

The following links were referenced:

Clone this wiki locally