Method Basics
Previously, we learned how to send information from one method into another using parameters. When we want to send a result back to the calling method, we must use a return. When a method returns information, that data needs to either be output or stored in a variable.
It is important to note the return type in the method signature. The return type is the type of information that the method returns. The return type is the type of data that the method will return to the caller. Return types can be int, String, double, etc. Methods that don't return information are labeled void.
Returns
Since the variables in a method are local to the method, altering information will not have any affect on data in the calling method. So, to be able to send information into a method and view the result of any computations/calculations in the caller, we need to return the data to the main.
Data is returned from a method using the keyword return. A method can only return one value per call. The type of information that the method is returning is specified in the signature. If a method is not going to return any data, the return type void is used.
Returning information is independent of parameters. In other words, method can return information regardless of whether or not it takes information in.
public static int getAge()
{
int myAge=16;
return myAge;
}
Finally, when a method that returns information is called, it is important that the information that is returned is stored in a variable of the appropriate type or placed inside of an output statement for display.
int hisAge=getAge();
OR
System.out.println("Her age is: "+getAge());