What is a parameterized reference in Java?

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

Was this solution helpful to you?

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

Just Added Q & A:

Find solution

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.