So now we know how to pay an arbitrary amount of our fees by using parameterized methods and arguments. This allows us to send information into a method. How would we send information back from the method?
For example, imagine that we want to be able to find out if a student has a scholarship. How would we do this?
The answer to this is another very important idea. Not only can we send information into the method, but we can tell the method to return information.
Let's illustrate this by changing the definition of our Student class to the following:
public class Student { public void reportFeesOwing() { System.out.println("Owing: " + feesOwing); } public void PayFees(double someamount) { feesOwing = feesOwing - someamount; } public boolean hasScholarship() { return scholarship; } private String name; private int age; private String courses; private double feesOwing = 5000.00; private boolean scholarship; }
What changes have we made? Look at the lines
public boolean hasScholarship() { return scholarship; }
Here we have defined a new method hasScholarship(), but instead of using the public void keywords that we've seen before, we're using public boolean. How does this change things?
Well, the boolean keyword indicates that the method hasScholarship() will return a value of type boolean. What this means is that when we call or invoke the method, we can expect it to give us back a value of type boolean.
How does a method 'give us something back'? Let's look at the syntax that is necessary to call such a method. A typical call would look like:
. . . Student s1 = new Student(); boolean schship; schship = s1.hasScholarship(); . . .
The dots above just refer to the fact that there can be other statements preceding and following the statements that are written. The important thing to notice is that we can think of a method that returns a value as replacing the method call with the value. Since we know that hasScholarship() returns a boolean we can think of the words s1.hasScholarship() as being replaced by the value that is returned by the method. This value is indicated by the statement return scholarship; in the definition of the method above; the value that is returned is the value of the instance variable scholarship.
So return is a keyword of the Java language, and the statement return xxx returns the value that is stored in the variable xxx.
Continuing with this line of thought, the boolean which has been returned will then be assigned to the variable schship by the statement schship = s1.hasScholarship();. Although we can think of the words s1.hasScholarship() as being replaced by the value of the variable scholarship in the code above, this is just a way of thinking about it. What really happens requires knowing a little bit more about the memory model, a topic to which we'll turn in the next section.
Before we do so, let me just mention that it is perfectly legal for a method to have parameters (yes! more than one!) and a return value. For example we could define our PayFees method as follows:
public double PayFees(double someamount) { feesOwing = feesOwing - someamount; return feesOwing; }
In this case, PayFees takes a parameter of type double and returns a value of type double.
We would call it as follows:
. . . Student s1 = new Student(); double AmountOwing; AmountOwing = s1.PayFees(1200.56); . . .
In this case calling PayFees would decrease the amount of feesOwing by 1200.56 and return the updated value stored in feesOwing, which would be 1200.56 less than it was before the method was called.
For the syntax of a method definition which has multiple parameters, look in your text, or at examples on the web.