How do I solve the problem of passing copy of reference of object, not actual reference, as an argument for a method in Java?
-
How can actual reference to an object to point null in a method, when we pass the object as an argument to a method? As Java always pass an object as a "copy" of actual reference rather than actual reference to object.
-
Answer:
Java does not have an "out" kind of parameter. You cannot change the reference used as a parameter, only the object referred to. If you are trying to signal that the object is no longer valid, this is commonly done with a "live" or "valid" boolean field on the object that can be set and tested by other code. Otherwise, you need to assign the return value to the reference you want to change., Eg myreference= MightMakeMeNull(myreference); MightMakeMeNull then returns either my reference or null. Edit: Actually there is one other VERY ugly wy to do this I should mention. You use a reference type and pass that in. eg public class MyReferenceType{ Object obj; public MyReferenceType(Object theActualObject){ set(theActualObject); } public Object get(){ return obj; } public Object set(Object O){ obj = O; } } SomeClass myObject = new SomeClass(); MyReferenceType rt = new MyRefenceType(myObject); FunctionThatMightSetRTToNull(rt); myObject = rt.get(); As I said, this is ugly and an awful lot of work in order to get a side effect, which is generally bad programming to begin with.
Jeff Kesselman at Quora Visit the source
Other answers
If I have understood ur ques right then u need to first create a new object in the method, copy all the attributes of the passed object's reference and use this new object. This way there would be no change to the original object.
Abhishek Sahu
Related Q & A:
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- After downloading attachments from emails i am not able to open them. How do i correct this problem?Best solution by Yahoo! Answers
- How do I solve the problem of downloading word document attached to my email?Best solution by support.google.com
- How do I solve a DNS error?Best solution by Yahoo! Answers
- How do I solve the coin problem?Best solution by Quora
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.