Method Parameter
Public void growBy(int amount)
The method header above tells us that the method receives one parameter of type “int”.
No Return Value
Java method(s) may or may not return a value. - The term “void” on the method header is used to indicate “no return value”.
Method Header Components
There are three components for Method Heads: 1. Method Return Type - means the method returns a value with the given return type (“int” in this case) 2. Method Name - the name of the method 3. Method Parameters - the method parameters that must be passes by the sender to this method.
Method Header
What is the Method Header? - The method header is the first line of the method and gives information about the method
Purpose of the Method Header - The purpose of the method header is to provide details to potential callers of the method. It would be very inefficient to require callers to dig through many lines of code to determine does. Instead, the method caller needs to only look at the method head. - Another name for method header is method signature
Methods with Parameters
Methods are like Functions, check below for an example:
Basic Function:
Function computeArea(width, height)
return width * height
Corresponding Java Method:
Public int computeArea(int width, int height) {
return width * height;
}
Introducing Methods
Example Below:
Public class Rectangle {
private int width;
private in height;
}
So, let’s add behavior to our object. Here’s an example simple method:
Public void setSmall() {
// Set ivars to arbitrary small values
width = 5;
height = 1;
}