Previous lesson 4/6 |home| Next lesson 6/6

Remote Method Invocation (Level II)

Parameters passing in RMI

The parameters passed to or a value returned to a remote object has a special requirement, because these values may travel a long distance to reach a receiver that may be a client or a server. Any object that is serializable is allowed to pass to or return from a remote object.

What is a serializable object?

Any object, including primitive types, remote objects and non-remote objects, that implements the java.io.Serializable interface is considered a serializable object.

What is the serializable interface?

The java.io.Serializable interface like java.rmi.Remote interface acts as a mark (it has no methods or fields) to tell the system such class or object is going to be downloaded to or travel on the network. All subtypes of a serializable class are themselves serializable.

The types we have dealt with in the previous programs are String, double, and int. Those types by default have been serialized.

How to find out a class has been serialized?

Check with Java API. Note that if the class itself or its super class implements the java.io.Serializable interface, that class has been serialized and it is safe to use in your RMI program.

But, any class you defined should be serialized if you want to use it in network communication. For example, you have a class Account like the below:

  
public class Account {
    private String name;
    private int AccountNumber;
    private double balance;
   
    ....	
}

And you have some methods in your remote class like:

  
private Account getAccount() throws RemoteException {
    return this.account;
}
or
private void setAccount(Account acct) throws RemoteException {
    this.account = acct;
}

In order to make your RMI program workable, you must make your class implement java.io.Serializalbe interface:

  
public class Account implements java.io.Serializable{
    private String name;
    private int AccountNumber;
    private double balance;
   
    ....	
}

So, the Account class is serialized and ready to be passed or returned to remote objects. Is that simple?

Note that when you download a serialized object (designed by your own), you may be required to set RMISecurityManager in your system.

Check your skill

A possible solution for wrapper classes: Go to Java API to find the answer.

If the class Object is serialized, its subclasses are serialized too. All the classes are the subclasses of class Object, therefore we don't need to worry about serialization. But the Java language is not designed in this way. So we need to keep serialization in mind.

Previous lesson 4/6 |home| Next lesson 6/6