Returning Arrays
Many of the methods we have written have the return type of void.
- This means they don't return anything.
public static void allSing(PopStar[] stars) {
//...
}
Other methods return data. * The type of the data is the return type for the method.
This method returns a String.
* It must return a String (or null).
public static String addExclamations(String s) {
String sExc = s + "!!!";
return sExc;
}
We can call the method and assign the returned data to a variable.
String output = addExclamations("Yo"); // output = Yo!!!
Returning Array References¶
Using the same process, we can return arrays references from our methods.
This method creates a String[] and returns a reference to the array.
public static String[] createList() {
String stringArr[] = {"Danny", "Donnie", "Joey", "Jon", "Jordan"};
return stringArr;
}
Again, we call the method and assign the returned reference to a variable.
String[] list = createList();
Practice Exercise¶
We can always tell what a method returns by looking at the return type. This is just what the Java compiler does to see if our method calls are legal.
The array is created inside a method, so why hasn't the array gone out of scope when the method ends?
- Inside the method, the array object is instantiated with the call to
new. This makes it exist outside the method. - The reference variable
stringArris like the address on a house. - When we return the
stringArr, we're returning a copy of the address. - The calling method gets that address, so it knows where the array lives.

- Java instantiates the array object.
createListreturns the reference to the array.- Array reference is stored in a variable outside
createListscope.
Drill¶
AdvancedArrays/com.example.advancedarrays.drills.ReturningArrays
* Add a method called public static PopStar[] createBand().
* Move the code that creates and fills a PopStar array into this method. Be sure to return the array from the method (since it's return type is PopStar[]).
* Call the method from main and store the array reference in a variable.
* Pass the array to the allSing method.
* Run the program.