I am a recent immigrant from c++ to Java. I have this problem:

I have an array of objects (not yet initialized using the new operator), that I need to pass to a member function of another class for storage. In c++ I would have made a pointer to the array, so as soon as I initialized the pointers in the array, the other class would automatically get it. How do I do this is Java?

6 Years
Discussion Span
Last Post by sbp94

Arrays/Objects ARE accessed using reference variables. Parameters are passed by value in Java, so if you pass an array to a method it is a copy of the reference to the Object or array that is passed not a whole copy of each element in the array. It goes to stand then that if I have an unintialised array as such, meaning the elements in the array haven't been initialised:

Integer[] myArray = new Integer[ 5 ]; // All of the elements are initially null

and pass myArray to some object that stores that reference variable, any changes made to the array outside the object will be visible inside the object and vice versa.

myArray[ 2 ] = 2;

The object storing the copy of the reference to the array will also see this change. It's pretty much similar to C.


But when I pass my array, the elements aren't even referencing to the objects. The objects are created afterward. But if the arrays are passed by reference, can I create another array that references the array I want to pass?

Because the problem is this: I want to pass a concatenation of two arrays, so I used the System.arraycopy method to copy the arrays, so will the change in the original two arrays still be visible in the other class.
If not is there another way to do this?


If not is there another way to do this?

Yes, have the method return that "new" array.

Java is pass-by-value and pass-by-value only. If you want pass-by-reference, use another language. If you want to use Java, then you need to abide by pass-by-value.


If it was a reference to the third/combined array that you had passed to the Object, then yes, changes will be visible. Personally I would prefer to have a "put" method which updates the Object with the concated arrays as it breaks encapsulation the way you are trying to do it and it may not be entirely clear what is going on to someone else looking at the code and you'd have to do extra work if the size of the arrays being combined isn't constant, so I'd:

concat to the two arrays to create a new combined array.
pass the combined array reference variable to the object to update it's "view"/reference of the combined array

public class MyClass{
   private int[] myArray;

   public void putArray( int[] combinedArray )
      myArray = combinedArray;  

That's the simple sort of way I'd do it.


Thanks a lot.
That solved my problem. I still wish Java had pointers that a programmer can use though.


Me too. They have their advantages, and careful use can make light work of otherwise difficult inelegant approaches. A well written program is a well written program pointers or not.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.