File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RMI pass by value or by reference ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "RMI pass by value or by reference ?" Watch "RMI pass by value or by reference ?" New topic
Author

RMI pass by value or by reference ?

Francesco Marchioni
author
Ranch Hand

Joined: Sep 22, 2003
Posts: 191
Hi all,
In view of the exam I did a little refresh about rmi.
As per definition "In RMI objects that implement the Remote interface are passed by reference. All other parameters are passed by copy "

So I started to code a minimal server which passes an object that implements java.rmi.Remote



Unfortunately the output isn't that I expected.

On the client I can see printed "0" and not 1000 like if
I have passed a reference of class "A" to the Server.

Can anybody shed some light on it ?
thanks
Francesco

[Andrew: tidied the code a little bit to make it easier to read]
[ November 28, 2005: Message edited by: Andrew Monkhouse ]

WildFly 8 Administration Book - JBoss Tutorials
Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284

Hello Francesco.

Exports the remote object to make it available to receive incoming calls using an anonymous port:


regards


SCJP, OCMJD, OCMJEA
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11528
    
100

Hi Francesco,

I could not find the definition you mention:
As per definition "In RMI objects that implement the Remote interface are passed by reference. All other parameters are passed by copy "
However I did find in the specification:
when an exported object of type UnicastRemoteObject is passed as a parameter or return value in an RMI call, the object is replaced by the remote object's stub. An exported remote object implementation remains in the virtual machine in which it was created and does not move (even by value) from that virtual machine. In other words, an exported remote object is passed by reference in an RMI call
Or, from the RMI Tutorial:
The rules governing how arguments and return values are passed are as follows.
  • Remote objects are essentially passed by reference. A remote object reference is a stub, which is a client-side proxy that implements the complete set of remote interfaces that the remote object implements.
  • Local objects are passed by copy, using object serialization. By default all fields are copied, except those that are marked static or transient. Default serialization behavior can be overridden on a class-by-class basis.
  • That last explanation is probably the simplest, and explains the problem you are seeing.

    Just stating that class A implements Remote does not mean that it will be passed as an exported Remote object - since you have not exported it (as indicated by Oricio) Java will revert to sending a copy of the local A instance.

    So lets convert your code to export A, which means that it will pass a remote object reference:Now when we test this, we can see that we get the same value on both client and server:




    Regards, Andrew


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Francesco Marchioni
    author
    Ranch Hand

    Joined: Sep 22, 2003
    Posts: 191
    Thank you for your replies.
    Actually I have read some documentation from a not official
    Java site (http://www.eli.sdsu.edu/courses/spring98/cs696/notes/rmiParameters/rmiParameters.html)

    Now it's quite clear.
    Regards
    Francesco
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: RMI pass by value or by reference ?