University of Cincinnati logo and link  
Passing Non Remote Objects
 
  UC ingot When we sent a message to the BulletinBoard, we created a String representing the message, and another String representing the user.  Then we sent a copy of these Strings to the server.  The server had its own copy, and could manipulate it as it wished, without affecting the Strings on the client.  We do this because String does not implement the Remote interface.
  • Normally in Java we pass by reference, where we send a memory location instead of a copy of the object.  (Actually this isn't 100% true, but we can say it is for our purposes).  Passing remote objects is passing by value, which is different, because we are passing a copy.
  • But what about more complex objects?  They can be copied if they implement interface serializable.
    • Take a look at the API documentation for serializable.  You'll see soemthing weird: no methods, no constants.  So why have the interface?  As stated in the documentation, "The serialization interface has no methods or fields and serves only to identify the semantics of being serializable."
    • If an object does not implement serializable, it is simply constructed with the default constructor when it reaches its destination.
    • Java is smart enough to marshall and unmarshall a serializable object by itself.  But, if you want special control over serialization, you must implement these methods:

    •  private void writeObject(java.io.ObjectOutputStream out)
           throws IOException
       private void readObject(java.io.ObjectInputStream in)
           throws IOException, ClassNotFoundException;
       


 Making a Serializable Object