I have multiple methods, each of which holds a reference to a single array. In one method where I have such a reference, I want to change the array to null. The problem is, I don’t just want to change one reference to null, but all of them. Does Java have a way of doing this?
public void moo()
{
boolean[] cow = new boolean[10];
baa(cow);
assertEquals(true, cow == null);
}
private void baa(boolean[] sheep)
{
*do stuff*
}
In other words, what can I do in baa() to sheep so that arr is null and the assert in moo is true?
Thank you!
The short answer is no you cannot.
cowis a reference to the actual array and you are passing a copy ofcowto the methodbaa. There is no way to change the original reference without using some kind of trickery.However, one trick you can use is to write your own wrapper class. This way you can pass around a reference to a reference to
cow.