Meaningless Drivel is fun!*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes query regarding pass by reference semantics of local business view from ivan's notes ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "query regarding pass by reference semantics of local business view from ivan Watch "query regarding pass by reference semantics of local business view from ivan New topic

query regarding pass by reference semantics of local business view from ivan's notes ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

the example given in the notes goes like this. please refer the example from the notes at page 74. i'm using latest version of the notes that has been update by ivan on slideshare

in the BEAN class :

in the servlet class :

ivan says that after we invoke processList method of bean class it adds a string namely "String added in EJB" which is visible from the servlet and hence it is pass by reference. but i couldn't get his point. java is pass by value. in this case the reference to the arraylist is passed to bean class method. in the bean class inList local variable still points to same object(i.e. arraylist) so any changes made by inList like addition , deletion will obviosly be visible in the servlet class also. this is normal java pass by value behaviour in my opinion.

i tried modifying the example as below :

since we were testing pass by reference semantics of Local business client view, if the parameters are passed by reference, inside the processList method of bean class i create a new arraylist and pointed the inList variable to it i.e.

now if it is pass by reference then the servlet will see the content of list as [a,b] rather than original set by servlet. right ? but when i tested the code it didnt worked as i assumed. in the servlet class i did not see any changes in list contents that i madey in bean method. instead the original contents were displayed. can somebody explained , why ?

so i have 2 doubts

1. is the ivan explanation correct regarding pass by reference. ?
2. why my code didnt worked as i expected ?

Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1502

Hi gurpreet,

Yes, technically, Java uses pass by value semantics. However, what it passes is - the reference. That is, while invoking method processList, a copy is made of 'reference to inList', and it is passed to processList.

Also, Ivan's notes put it correctly. Now, since we pass a copy of the reference, the moment you change that copy of a reference (to refer to some another list), it is lost, and this new list won't be available to be accessed from outside the method.

Basically, what the spec (and Ivan's notes) say is:
1) When local view of an EJB is used, then copy of the reference is made (thus, both references refer to same 'inList'). And hence, if a change is done to inList inside the method, it will be also visible outside the method. This is possible only because it is a local call, and thus we are not crossing JVM boundries.
2) When remote view of an EJB is used, then the whole inList is serialized, sent from one JVM to another JVM(which may or may not be on same physical machine). Thus, in new JVM, inList has new reference, and now we have two references to two different inList (which belongs to two different JVMs). Thus, change made by a reference in one JVM (e.g. via processList method) is not visible in another JVM (unless the list is explicitly returned, but here, processList returns void). You can try the same piece of code with remote view and you won't find 'String added in EJB' in the list - because it was added in another JVM, and the reference was not returned to original JVM.

I hope this helps.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
I agree. Here's the link:
subject: query regarding pass by reference semantics of local business view from ivan's notes ?
Similar Threads
Using logic:iterate in select box
Passing by value vs. reference
Remote Java EE Client with JNDI Lookup for Remote Session Beans
Displaying AWT widgets in Applets
Bubble Sort Set using Comparator