• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
  • Carey Brown
  • salvin francis
  • Piet Souris

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

Ranch Hand
Posts: 924
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

Posts: 1558
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic