CONTENTS | PREV | NEXT | Java Remote Method Invocation |
The default implementations in the Object class for theequals
,hashCode
, andtoString
methods are not appropriate for remote objects. Therefore, the java.rmi.server.RemoteObject class provides implementations for these methods that have semantics more appropriate for remote objects. In this way, all objects that need to be available remotely can extend java.rmi.server.RemoteObject (typically indirectly via java.rmi.server.UnicastRemoteObject).
In order for a remote object to be used as a key in a hash table, the methodsequals
andhashCode
are overridden by the java.rmi.server.RemoteObject class:
- The java.rmi.server.RemoteObject class's implementation of the
equals
method determines whether two object references are equal, not whether the contents of the two objects are equal. This is because determining equality based on content requires a remote method invocation, and the signature ofequals
does not allow a remote exception to be thrown.- The java.rmi.server.RemoteObject class's implementation of the
hashCode
method returns the same value for all remote references that refer to the same underlying remote object (because references to the same object are considered equal).
ThetoString
method is defined to return a string which represents the reference of the object. The contents of the string is specific to the reference type. The current implementation for singleton (unicast) objects includes information about the object specific to the transport layer (such as host name and port number) and an object identifier; references to replicated objects would contain more information.
Objects are only cloneable using the Java language's default mechanism if they support the java.lang.Cloneable interface. Remote objects do not implement this interface, but do implement theclone
method so that if subclasses need to implement Cloneable the remote classes will function correctly.Client stubs are declared final and do not implement
clone
. Cloning a stub is therefore a local operation and cannot be used by clients to create a new remote object.
Remote object implementations (subclasses of RemoteObject) can usefinalize
to perform their own cleanup as necessary. For example,finalize
can be used to deactivate an object server.