CONTENTS | PREV | NEXT Java Remote Method Invocation


5.1 The RemoteObject Class

The java.rmi.server.RemoteObject class implements the java.lang.Object behavior for remote objects. The hashCode and equals methods are implemented to allow remote object references to be stored in hashtables and compared. The equals method returns true if two Remote objects refer to the same remote object. It compares the remote object references of the remote objects.

The toString method returns a string describing the remote object. The contents and syntax of this string is implementation-specific and can vary.

All of the other methods of java.lang.Object retain their original implementations.

package java.rmi.server;

public abstract class RemoteObject implements java.rmi.Remote, java.io.Serializable { protected transient RemoteRef ref;

protected RemoteObject(); protected RemoteObject(RemoteRef ref);

public RemoteRef getRef(); public static Remote toStub(java.rmi.Remote obj) throws java.rmi.NoSuchObjectException; public int hashCode(); public boolean equals(Object obj); public String toString(); }

Since the RemoteObject class is abstract, it cannot be instantiated. Therefore, one of RemoteObject's constructors must be called from a subclass implementation. The first RemoteObject constructor creates a RemoteObject with a null remote reference. The second RemoteObject constructor creates a RemoteObject with the given remote reference, ref.

The getRef method returns the remote reference for the remote object.

The toStub method returns a stub for the remote object, obj, passed as a parameter. This operation is only valid after the remote object implementation has been exported. If the stub for the remote object could not be found, then the method throws NoSuchObjectException.


5.1.1 Object Methods Overridden by the RemoteObject Class

The default implementations in the java.lang.Object class for the equals, hashCode, and toString methods are not appropriate for remote objects. Therefore, the RemoteObject class provides implementations for these methods that have semantics more appropriate for remote objects.


equals and hashCode methods

In order for a remote object to be used as a key in a hash table, the methods equals and hashCode need to be overridden in the remote object implementation. These methods are overridden by the class java.rmi.server.RemoteObject:


toString method

The toString method is defined to return a string which represents the remote reference of the object. The contents of the string are specific to the remote reference type. The current implementation for singleton (unicast) objects includes an object identifier and other information about the object that is specific to the transport layer (such as host name and port number).


clone method

Objects are only clonable using the Java programming language's default mechanism if they support the java.lang.Cloneable interface. Stubs for remote objects generated by the rmic compiler are declared final and do not implement the Cloneable interface. Therefore, cloning a stub is not possible.


5.1.2 Serialized Form

The RemoteObject class implements the special (private) writeObject and readObject methods called by the object serialization mechanism to handle serializing data to a java.io.ObjectOutputStream. RemoteObject's serialized form is written using the method:

private void writeObject(java.io.ObjectOutputStream out)
	throws java.io.IOException, java.lang.ClassNotFoundException;

A RemoteObject's state is reconstructed from its serialized form using this method called by the ObjectInputStream during deserialization:

private void readObject(java.io.ObjectInputStream in) 
	throws java.io.IOException, java.lang.ClassNotFoundException;

CONTENTS | PREV | NEXT
Copyright © 1997-1999 Sun Microsystems, Inc. All Rights Reserved.