wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Arguments and results of methods of local business interface are passed by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Arguments and results of methods of local business interface are passed by reference" Watch "Arguments and results of methods of local business interface are passed by reference" New topic
Author

Arguments and results of methods of local business interface are passed by reference

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
On Frit's notes p.6 and 7 :

1.1.2 Local Client
The arguments and results of the methods of the local business interface or local no-interface client view are passed by reference.

1.1.3 Remote Client
The arguments and results of the methods of the remote business interface are passed by value.



For the remote client, it is understandable why the arguments and results are passed by value. The arguments are copied and the copy of the values are serialized before it is sent to the bean in the remote JVM.

But why the arguments for the local interface client view are designed as passing by reference? Java does not support pass by reference.
Mike Degteariov
Ranch Hand

Joined: Oct 03, 2007
Posts: 62
    
    8

But why the arguments for the local interface client view are designed as passing by reference? Java does not support pass by reference.

For remote interface view, parameters are serialized by the container before sending to the wire, and then they get deserialized from the wire into new object on receiving side.
If you have a mutable object with a state, and you pass that object as a parameter to remote EJB, and after you have completed the remote call you will change the state of the object on client side - the object received on remote side will remain unaffected. Because these are two different objects now.

For the local interface and no interface view, the object is passed in a same way as in Java SE. That is, object is passed by reference and reference is passed by value. As a result, both sender and receiver will have the same object, and changes made with the object by either sender or receiver part will affect the opposite part.


Mike Degteariov
SCJP5 SCWCD5 SCBCD5
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775

For the local interface and no interface view, the object is passed in a same way as in Java SE. That is, object is passed by reference and reference is passed by value. As a result, both sender and receiver will have the same object, and changes made with the object by either sender or receiver part will affect the opposite part.


Hi, Mike. Thanks for your explanation.
Suppose we have this:

In this way, if myObject refers to something else, both sender and receiver will be affected.
Mike Degteariov
Ranch Hand

Joined: Oct 03, 2007
Posts: 62
    
    8

Sorry, what you show here is called "function with side effects" and is bad programming practice indeed.
In this case, however, the receiver (myLocalBean) will not be affected and his string will still be "Hello".
Note that on line 15 you change the reference and not the content of the string. And just to make sure we are clear here - strings are immutable in Java and you cannot change the content of java.lang.String once it's created.

Hope it helps.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
Sorry. That was a bad example. Let me clarify it:

I haven't get to the point to try out example code yet. But I think that is the proof of concept.
 
 
subject: Arguments and results of methods of local business interface are passed by reference