Q:

Array and methods?

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);

A:

Arrays in Java are objects. If you modify the trans array inside the method, the changes will be reflected outside of it1. Eg:
public static void modify(int[] arr)
{
    arr[0] = 10;
}

public static void main(...) 
{
    int x = {1, 2, 3};
    System.out.println(x[0]); // prints 1

    modify(x);
    System.out.println(x[0]); // now it prints 10

}

Note that native arrays can't be dynamically resized in Java. You will have to use something like ArrayList if you need to do that. Alternatively you can change the return type to int[] and return a new array with the new element "appended" to the old array:
public static int[] makeTransaction(int[] trans, int amount)
{
    int[] new_trans = Arrays.copyOf(trans, trans.length + 1);
    new_trans[trans.length] = amount;

    return new_trans;
}

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:
public void no_change(int[] arr)
{
    arr = new int[arr.length];
}

