Sub topic 1
Declare and Call a Method
A method is a named set of instructions that performs a task. You can write those instructions once and run them whenever you need them. This helps keep a program easier to read and change.
To use a method, you need two things:
- A declaration tells Java the method’s name and what it does.
- A call asks Java to run the method.
A first method
A class is a named container for Java code. This class declares a method called showMessage and calls it twice:
public class MethodsDemo {
public static void main(String[] args) {
showMessage();
showMessage();
}
static void showMessage() {
System.out.println("Welcome!");
}
}Save the code in a file named MethodsDemo.java. The file name matches the public class name, including its capital letters.
To run it, you need a Java Development Kit (JDK), a set of tools for writing and running Java programs. Open a terminal, a window for typing commands, in the folder containing the file. Then run:
javac MethodsDemo.java
java MethodsDemoThe first command translates the code into a file that Java can run. The second starts the program. You should see:
Welcome!
Welcome!The message appears twice because the program calls showMessage() twice.
Follow the calls
Java starts this program at the main method. Follow the order of events:
maincallsshowMessage().- Java runs the instructions inside
showMessage. showMessagefinishes, so Java continues after the call inmain.maincallsshowMessage()again.- When that call finishes,
mainfinishes too.
The instructions for showMessage are written only once. Each call runs those same instructions.
Inside showMessage, the line System.out.println("Welcome!"); calls a method provided by Java. It prints the text between the double quotes on a new line.
Read the declaration and call
Look at the declaration again:
static void showMessage() {
System.out.println("Welcome!");
}Each part has a job:
staticmeans the method belongs to the class. That letsmaincall it directly here.voidmeans the method does not give a value back to its caller.showMessageis the method’s name.()shows that this method needs no input.- The braces,
{and}, hold the method’s instructions. This part is called the method body.
Now compare the call:
showMessage();The call uses the method’s name and parentheses. It ends with a semicolon. It does not include the body: Java finds the declared method and runs its body.
The main declaration has a few extra parts. public allows Java to access it when starting the program. String[] args provides a place to receive text supplied when the program starts. You do not need to use that text in this example.
Try a change
Change the text inside the double quotes to a different message. Save the file, then run both commands again. Both lines of output should show your new message.
If the program does not run, check these common mistakes:
- The class name and file name must match:
MethodsDemoandMethodsDemo.java. - The spelling and capital letters in
showMessage()must match its declaration. - Each call must end with a semicolon.
- The
showMessagedeclaration must be inside the class, but outside the body ofmain.
Exercise
What happens if you remove one of the two showMessage() calls from main and run the program again?
Show answer
Welcome! is printed once, because main now calls showMessage only once.