I’m doing a task for a course in Java programming and I’m not sure how the following thing is working? The method below takes the value from an array and a integer. The integer should be added to the array and then be used outside the method in other methods and so on, but how could this work when the method has no return for the new content of the array? There is a void in the method? Have I missed something? Preciate some help? Is there something about pointers?
public static void makeTransaction(int[] trans, int amount);
Arrays in Java are objects. If you modify the
transarray inside the method, the changes will be reflected outside of it1. Eg:Note that native arrays can’t be dynamically resized in Java. You will have to use something like
ArrayListif you need to do that. Alternatively you can change the return type toint[]and return a new array with the new element “appended” to the old array:1 It is also worth noting that as objects, array references are passed by value, so the following code has no effect whatsoever outside of the method: