first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

Java Fundamentals: “Is Java pass by reference?”; stackoverflow.com

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

It goes like this:

public void foo(Dog d) { d.name == "Max"; // true d = new Dog("Fifi"); d.name == "Fifi"; // true }

Dog aDog = new Dog("Max"); foo(aDog); aDog.name == "Max"; // true

In this example aDog.name will still be "Max". "d" is not overwritten in the function as the object reference is passed by value.

Likewise:

public void foo(Dog d) { d.name == "Max"; // true d.setname("Fifi"); }

Dog aDog = new Dog("Max"); foo(aDog); aDog.name == "Fifi"; // true

[http://stackoverflow.com/questions/40480/is-java-pass-by-reference][erlando]

mod date: 2008-12-02T21:34:20.000Z