This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to make the reference object also null when the method makes its parameter null value?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I have a code like this


what i want to make is when i make obj= null inside the method then it should reflect rObj also.
 
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

santhosh.R gowda wrote:

what i want to make is when i make obj= null inside the method then it should reflect rObj also.



Well, I don't think that is possible... 'cause the parameter passing is by reference and not by actual object, so a change to reference in the obj will not reflect back to rObj... this will simply change obj's pointng direction to another direction which is null in this case.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not how Java works. You'd have to pass in a wrapper.
 
Anupam Jain
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Newton wrote:That's not how Java works. You'd have to pass in a wrapper.



Hello David,
Could you please elaborate that a little, please? What's a wrapper got to do with it... or... how to use it??
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the method set foo.theRef to null, and use foo.theRef in the main method.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
I don't understand this wrapper concept.Can you please enlighten me with a little more explanation.
What if try returning the nullReference object as I have done in the below code .

 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the sole purpose of a method is to set some variable to null, then why not just set it to null??
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Yogesh: The point is that you can't set a method's parameter to null and expect it to be reflected in what called that method. Your code does the same thing your original code did, but with my wrapper object instead. The idea was to pass in the wrapper and set its *instance variable* to null, if that's what your usecase demands.

What, specifically, are you trying to do? All my solution does is solve the problem of needing to set something to null and have access to it later, it doesn't change the way Java works.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the simplest of all !! but I guess Santosh would have complex logic implemented in that method .
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic