• Post Reply Bookmark Topic Watch Topic
  • New Topic

"passing by reference" in functions  RSS feed

 
Lisa Cook
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i assign values to null parameters in functions?
for example:

i dont know why that wont work.... i assigned it to an instance in the 2nd function, so why would it still be null??
the error i get is the null pointer exception when i try to excute
a.method()
Thanks for any help provided
[ November 18, 2003: Message edited by: Lisa Cook ]
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why they say that in Java there is only pass by value.
When you call the other method, what was passed was the value of your object
reference.
So what happen was:
1. a = null
2. you passed a copy of a reference to a method
3. that method has it's own 'a'(unfortunate that you pick same name), that
has a copy of original 'a' value which is null.
4. You reassign copied 'a' inside the method to an object.
Original 'a' still has null value.
5. method exit and object that was referenced by copied 'a' went out of scope
6. original 'a' is still null when you call it.
Hope that helps..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!