Methods
A method is a block of code enclosed with a descriptive name that describes what the enclosed code collectively does. It can be run by invoking the method name elsewhere in the project. The methods are compelling programming features that abstract a highly complex block of code down to a single method name.
In this code note, we will cover following topics:
- Benefits of Method
- Method Structure
- Creating Methods
- return keyword
- Method Signature
› Benefits of Method¶
Advantages of defining methods in a program are:
- It organizes repeating code.
- It enables the creation of a reusable code block
- It can be called numerous times and in multiple different places in the project.
- Users can input data for processing into the method.
- It will return processed data to the users.
Instead of writing test case like this:
You can write simple test case like this:
| with using a method | |
|---|---|
› Method Structure¶
Different parts of the Java method:
Following dropdowns explains each part of the method:
Access Modifier
- Determines whether other codes in the same project can use this method.
- It has a few variants: public, private, protected, which we will learn later.
Return Data Type Specification
- Shows the type of data returned from the method to the user.
- Use
voidto indicate the method does not return any data to the user.
Method Name
- The name of the method should describe what the enclosed code collectively does.
- Naming rules and conventions are the same as variables.
Parameters
- Temporary variables used for storing input data values provided by the method caller.
- Do not list any parameters if the method does not need input data from the user
Method Body
- It consists of zero or more statements (executable code).
- It starts with code open block
{and ends with closing code block}.
› Creating Methods¶
You can create 4 types of methods in Java. They are distinguished by whether or not they take input data from the user. Also, whether or not they return processed data to the method user.
Code example: Creating Method
Example of creating 4 different types of methods.
Code example: Method Calling
In the same Java document, we can use the methods we have created by simply invoking its method name and providing input data.
› Method Parameters¶
The method parameters are used for storing users' input data value to the method. Without parameters, creating a robust method is not possible as we have to create many methods that cover all the possible user input data one by one. Consider the following example where getRandomNumber(int) is invoked inside the main method.
› return Keyword¶
Java has a special keyword for exiting a method with data. It is the return keyword, which can only be used inside the method.
Consider the canIvote(29) method below. Whenever Java finds this method name, it will get into the method body to execute the block of code. Whenever a return keyword is encountered, the Java will exit from the method body and goes back to the method call statement.
Inside the method, we can put the data we would like to give back to the caller right after the return keyword. However, the type of data we put must match with the specified Return Data Type Specification of the method.
List of scenarios when the Java will exit from a method:
Java would exit from the method body during code execution if any of this took place.
- End of the method is reached: no more code remains in the method that needs to be executed.
- return statement encountered:
returnstatement has been encountered with datareturn 19;or without datareturn;it will exit. - Error or Exception occurs: method abruptly exits when executed code results in an error.
› Method Signature¶
When you create methods, be careful not to duplicate them. It will result in programming errors since Java cannot decide which one of them to execute. However, simply having two methods with identical names does not make them a duplicate method.
In Java, two methods are only considered duplicate if and only if all of the following four properties are exactly the same between them. These four properties are collectively known as the method signature:
Method Name
Number of parameters
Data Type of each parameter
Appearing order of each parameter
The following methods are all different, except for the last one. Please read the comments for detailed explanations
There is a Java programming concept called Method Overloading that uses method signature heavily; however, we will learn them in later sessions.