• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Himai Minh
Ranch Hand
Posts: 1141
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
8
Java Mac MySQL Database Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Himai Minh
Ranch Hand
Posts: 1141
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 143
8
Java Mac MySQL Database Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic