Java Programming Home Page: Archive: Message #79

Date: May 15 2000 14:39:33 EDT
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: call by value

Hi All,
There were a couple of questions regarding Java's
call by value.
When Java passes a reference, the logical reference is
copied. Thus, even if the local copy of the reference is overwritten,
the caller will not have its reference altered. However,
the data that is being referenced CAN be altered, if permission
is granted.

Thus:
public void f(Object o) {
  o = null;
}
will not affect the reference passed when:
     f(s) is called (s still equals s).
If s needs protection, it is done with visibility modifiers
on the setters and getters (this is why Java does not need
const).

If you really want to protect the Object from being set, then use:
public void g(final Object o) {
  o = null; // generates a syntax error
}
Keep in mind, keeping the reference final does NOT prevent the
called method from setting properties. Only altering the
visibility modifiers can do that.
 - DL