Search⌘ K
AI Features

Simple User-defined Methods

Explore how to define and call simple user-defined methods in Java. Understand the method structure including headers and bodies, learn execution sequences, and practice creating reusable code modules to improve programming organization and efficiency.

Methods

We can divide a program into procedural components or modules called methods in Java. We are already familiar with the main() method. Methods can be copied and reused in a programming technique called a modular or procedural approach, also known as the divide and conquer approach. We should always try to design methods that can be reused in other programs too.

Broadly, there are two types of methods, which we’ll explore in the following sections.

The structure of a method

The following slides illustrate various parts of a method definition:

A function name can only contain letters (AZ and az) and the underscore symbol (_). It may also contain digits (09), but the function name can’t start with those. For example, Key_2 is a valid function name, but 2_key is not. Function names are case-sensitive, meaning that name, Name, and NAME are three different functions.

There are two main parts of a method definition:

  • Header: The first line, which ends with a closing parenthesis ).
  • Body: The block of statements below the method header, starting with an opening brace { and ending with a closing brace }.

The method header contains the keywords static and void (void means that the method does not have a return value), the method name sayHello, and the parameters in (). All the statements in the method body are written as a block of code enclosed in { and }. We can not write the body of the method without braces.

Method call

The following program demonstrates the creation of a method and a method call.

Java
import java.util.Scanner;
class Test
{
static void sayHello() //Header of the void method
{
System.out.print("Hello"); //printing Hello
}
public static void main(String[] args)
{
sayHello(); //calling a method
}
}

We can call a method by writing the method name, followed by () and ending with ;. In line 11, we tell the program to call the method sayHello(). This line is known as a method call or calling the method.

Execution sheet for method calls

Let’s look at the following code before working on its execution sheet:

Java
class Test
{
static void first()
{
System.out.println("Now inside the method 'first'.");
}
static void second()
{
System.out.println("Now inside the method 'second'.");
}
public static void main(String[] args)
{
System.out.println("Main program is starting here.");
first(); //calling first method
System.out.println("Back in main program.");
second(); //calling second program
System.out.println("Back in the main program again.");
}
}

In the program:

  • We define two simple methods—the first and second.
  • We demonstrate the sequence of execution with the help of System.out.println() statements.

The most important column to understand the sequence of execution is the PS# column. The sequence of PS# for the above program is 12,14,15,3,5,16,17,7,9,1812, 14, 15, 3, 5, 16, 17, 7, 9, 18.

Here’s another code to help us understand the execution sequence of method calls. The sequence of PS# for the following program is 14,16,17,7,9,10,3,5,11,1814, 16, 17, 7, 9, 10, 3, 5, 11, 18.

Java
class Test
{
static void deeper()
{
System.out.println("Now inside the method ''deeper'.");
}
static void deep()
{
System.out.println("Now inside the method ''deep'.");
deeper(); // Call method deeper
System.out.println("Now back in ''deep'.");
}
public static void main(String[] args)
{
System.out.println("Main program is starting here.");
deep(); // Call method deep
System.out.println("Back in the main program.");
}
}

The execution sequence can also be visualized as follows:

The illustration above shows the PS# and the depth of each method call more clearly. The first two statements are from the main program, the next three are from the method deep(), and the next one from the method deeper(). The last one is again from the main program. The main program is the caller of the deep() method. The deep() method is the caller of deeper().

Practice creating and calling methods

Here are a few example programs to practice creating and calling methods. By clicking the “Show Solution” button, you’ll find a program that solves the respective problem. You can copy and paste the given solution into the code widget to make sure that the output of your solution matches the given solution. There can be several ways of writing correct solutions in programming.

Method to create a multiplication table

Write a method showTableOf4() that displays 20 terms from the table of 4. Call your method from the main() function to display the results.

Sample output

4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
4 x 11 = 44
4 x 12 = 48
4 x 13 = 52
4 x 14 = 56
4 x 15 = 60
4 x 16 = 64
4 x 17 = 68
4 x 18 = 72
4 x 19 = 76
4 x 20 = 80
Java
class Test
{
//Write your code here
public static void main(String[] args)
{
}
}