Session 2 |
Methods
Occasionally a certain portion of code has to be used many times. Instead of re-writing the codes many times, it is better to put them into a subroutine, and call this subroutine many times. As a result, the code becomes shorter, easier to maintenance and understand. Such subroutine is called a method (in Java) or a function (in Python).
In this session, we will focus on static methods (use the modifier static
). These methods do not deal with objects; all the data is passed through parameters. Note that public static methods can be called out in other classes using the class name, e.g. Math.max(1, 10)
.
Apart from variable, methods also have a data type. If a method does not return anything, its data type is void and the method is called a void method. If a method returns a value, the method has the correspond data type and the method is called a value method.
void methods
The void methods are methods which do not return a value. An example of such is the main method in HelloWorld program:
public static void main(String[] args)
To define a void method, use the following syntax:
public static void myMethod(list_of_parameters) { //statements; }
The first line of a method is called the method header and it contains some specific keywords:
Modifier
public
indicates that this method can be used by all parts of your program.static
indicates that this is a procedural-style (not object- oriented) method (for now, all methods that we write are static until we learn about defining objects - next session).
Return value type
void
indicates that this method executes statements but does not produce any value (other methods we will see later compute and return values).
Method name
myMethod
is a name of a method. Generally, it is a good idea to start a method’s name with a verb (e.g., calculate) followed by an item of the operation (e.g., calculateAge). Do not forget about the camel case: start the first word with a lower case letter, and capitalised the rest of the words with no spaces.
Formal parameters or simply parameters
list_of_parameters
specify a list of values that are sent to the method as an input. The parameters in the list must be defined separately, each one with its data type separated by comma. A list of parameters is optional: a method may have none (empty parentheses), one or more parameters (separated by commas).
Example of method definition:
public static void multiplyThreeNumbers(double a, double b, double c) { System.out.println(a*b*c); }
Note that methods can be placed before or after the main method, but not inside the curly brace of the main method!
Just defining a method will not actually tell Java to execute it. To run the code inside the method, it has to be called/invoked by its name.
Example of a method call:
multiplyThreeNumbers(4.5, 3.23, 9.0);
Each method invocation statement starts with the name of the method followed by actual parameters in a pair of parentheses finishing with a semicolon. Note, that the order of the actual parameters (or simply arguments) must be exactly in the same order as the parameters in the method declaration.
When a method is called, Java jumps to that method definition, runs all the code inside that method, then returns to the line where it was called.
Another example of void methods:
public class Hello { public static void printNum(int a) { System.out.print(a + ". "); } public static void helloWorld() { System.out.println("Hello, World!"); } public static void main(String[] args) { for (int i = 0; i < 5; i++) { printNum(i); helloWorld(); } } }
In this program, the void method printNum is called to print a number. The actual parameter i is passed to the formal parameter a in the method and the method prints the number. Then the program returns to the main method and calls the next void method - helloWorld. This method does not have any actual parameters. On each iteration, the program prints a number and the text Hello, World!.
Self-assessment
Session 2 |