If I write out an array the long way in a seperate class such as
public Student [] getArray(){
Student [] studentArray = new Student[3];
studentArray[0] = new Student (”Mel”);
studentArray[1] = new Student (”Jared”);
studentArray[2] = new Student (”Mikey”);
return studentArray;
}
Will the return statement return all the names to my other class that I’m actually going to run, or just one?
Here, the
returnstatement will return the entire array, which means that the caller can access all three of theStudentobjects. For example:If you want to return just one
Student, then your return type would beStudentand you would have to specifically pick which one to return. In Java, returning an array always returns the entire array, and you don’t need to say that you’re returning all of the contents with it.Hope this helps!