Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I return a different object in a method parameter?

0
Posted

How can I return a different object in a method parameter?

0

Ans : There are two ways. The obvious way is “just add another level of indirection”. Wrap the object in another class, whose purpose is simply to be passed as a parameter, allowing the nested object reference to be modified. The second alternative is a clearer variant of this. Pass in a single element array. Since arrays are objects, this works. void jfoo(Object ref[]){ ref[0] = new Object(); } … Object kludge[] = new Object[1]; kludge[0]= myObj; jfoo(kludge); if (kludge[0] == myObj) … else … Note that changing a global variable/object inside a method is an egregious programming practice; it usually violates basic OOP constructs.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.