Is "pass by value" enforced for calls within the same VM to objects that implement java.rmi.Remote? In other words, if I'm writing an object that implements java.rmi.Remote, can I assume that calls to it that originate within the local VM will enjoy the same "copy by value" rules for serializable parameters?
Created Feb 1, 2000
Tim Rohaly
But, if we obtain a remote reference
to the object, for example:
Say I have a remote interface:
public interface Hello extends Remote {
public String sayHello() throws RemoteException;
}
and an implementation like:
public class HelloImpl extends UnicastRemoteObject implements Hello {
public String sayHello() {
return "Hello!";
}
}
Here, the return parameter of the sayHello() method is a
serializable object (String).
If you obtain a reference by simply instantiating HelloImpl, then its semantics are that of a normal Java object. i.e., if you do:
Hello hello = new HelloImpl(); String result = hello.sayHello();then the "result" reference points to the same object as the local String object created within HelloImpl.sayHello() - a copy of the String object is not made.
Hello hello = (Hello) Naming.lookup("rmi://localhost/helloserver");
hello.sayHello();
then the semantics are different, because we don't have a normal reference
created by "new", we have a remote reference obtained by contacting the
RMI registry. In this second case, the result string is serialized and
returned, effectively creating a copy. Note that this is true whether
or not the actual implementation object lives in the same VM. The
deciding factor is how the reference was obtained.