
Methods
Methods are very useful. It can be used to break a complex program into small manageable pieces. A method is simply a block of statements that has a name and can be executed by calling it from some other place in the program. A void method simply executes a group of statements and then terminates. A value-returning method returns a value to the statement that called it. So far we have created a method named main in every program we've written, and we've executed predefined methods from the java API, such as System.out.println, Math.pow and so on. In this section, we will learn how to create our own methods, and how to execute them. We will focus on the void method and value-returning methods.
Defining a void Method
To create a method we must write
its definition, which consists of two general parts: a header and a body. We
learned about both of these in previous chapters.
Here is a review of how a header and a body is like
![]()
Header
public static void main(String[] args)
{
Body
System.out.println("Hello World");
}
Now lets see an example how we are going to write a simple void method
public static void display()
{
System.out.println("Hello World From the Method");
}
* public: The keyword indicates that the method's is able to use by any Java program that knows about the method.
* static:
This keyword declares that the method is a static method, which means that it
can be called without creating an instance of the class in which it's defined.
The main method must always be static, and any other methods in the class that
contains the main method.
* void: The keyword void is our return type. Simply, it means the method doesn't return any value.
* display: It's the name of our method. display(), will be use whenever we want to call the method.
* (): The parentheses can hold parameters, we can pass value into the method and be able to uses it.
* Method Body: The rest is the method body, which consists of executable statements such as if, while, and for.
Now, lets see some examples on calling methods....
Void Method Examples
/*
The program calls a simple method
*
*/
public class SimpleDisplay
{
public static void main(String[] args)
{
System.out.println("Hello World From
Main\n");
// Here we call the Display() method
Display();
System.out.println("Hello World From
Main again\n");
}
public static void Display()
{
System.out.println("Hello World From
the Method\n");
}
}
Click here to download SimpleDisplay.java
Notice, when we declare the method, it has to be in the same class. To call the method in the main, we simply type the name of the method.
Lets observe another void method example
/* This program calls a simple method
using a loop
*
*/
public class LoopMethod
{
public static void main(String[] args)
{
System.out.println("This is a hello
from the main");
// Here we uses a for loop to loop
the method
for(int i=0; i<4; i++)
{
display();
}
System.out.println("Now we are back
to main");
}
public static void display()
{
System.out.println("This is a hello
from the method");
}
}
Click here to download LoopMethod.java
Now lets see another unique example, a void method inside another void method
/*
This program simply demonstrate how to call a void method
inside a void method
*
*/
public class DoubleMethod
{
public static void main(String[] args)
{
System.out.println("I'm in main right
now");
// Here we calls the first method
method1();
System.out.println("Now I'm back in
main again");
}
public static void method1()
{
System.out.println("Hi! I'm in the
first method right now");
// Now we calls the second method
method2();
}
public static void method2()
{
System.out.println("Now I'm in the
second method");
}
}
Click here to download DoubleMethod.java
Now, lets look at value returning method
A value method is basically a method that returns a value and we are able to uses its value to do whatever we want. Here is an example
/*
The program does simple addition
It uses a method, asks the user for two integers
Then it sums up the integers and returns the sum
Finally, it prints out the sum in the main method
*
*/
import java.util.Scanner;
public class MethodAdd
{
public static void main(String[] args)
{
// Prints out the Sum
System.out.println("The sum is: " +
Sum());
}
/*
This is a value return method
It asks the user for two integers
Then it sum up the integers and
returns the sum
*
*/
public static int Sum()
{
Scanner key = new Scanner(System.in);
int x, y;
int sum = 0;
System.out.println("Enter the first
integer: ");
x = key.nextInt();
System.out.println("Enter the second
integer: ");
y = key.nextInt();
sum = x + y;
return sum;
}
}
Click here to download MethodAdd.java
Note, here we have public static int Sum(), where int determines the type we are returning. Then we call the value as how we call the void method.
Lets observe a different return-value method.
/*
This program returns a String
*
*/
import java.util.Scanner;
public class NameReturn
{
public static void main(String[] args)
{
System.out.println("The name you
entered is " + name());
}
public static String name()
{
Scanner key = new Scanner(System.in);
String name;
System.out.println("Please enter a
name: ");
name = key.nextLine();
return name;
}
}
Click here to download NameReturn.java
Methods That Take Parameters
A parameter, is a value that we can pass to a method. The method then can use the parameter as if it were a local variable initialized with the value of the variable passed to it by the calling method. For a method that accepts parameters, it must list the parameters in the method declaration. The parameters are listed in a parameter list that's inside the parentheses that follow the method name. For each parameter used by the method, we must declare its type inside the parentheses. If more parameters are needed, we must separate the parameters by a comma. All these talking must be confusing, although try and remember what we just read and follow through the examples below.
/*
The program demonstrates the use of parameters
*
*/
public class MinMax
{
public static void main(String[] args)
{
int min = 1;
int max = 5;
/*
Pass min and max as a paramenter into getSum
Assign the value into number
*
*/
int number = getSum(min, max);
System.out.println("The sum of min and max is " + number);
}
/*
Returns the sum of min and max
*
*/
public static int getSum(int min, int max)
{
return min + max;
}
}Click here to download MinMax.java
This is call scoping out parameters, which means a parameter can have the same name as local variable used in other methods without causing any conflict. Hence, the above program we have min and max declared as local variable, in addition we declared min and max in the parameter list.
Pass by value
/*
This program demonstrates pass-by-value
*
*/
public class PValue
{
public static void main(String[] args)
{
int number = 3;
changeNum(number);
System.out.println(number);
}
public static void changeNum(int i)
{
i = 1;
}
}Click here to download PValue.java
Here we have a number variable set to 3 and then pass to the method name changeNum. The method receives the variable as a parameter named i, and then sets the value i to 1. Then back in main, we are trying to print out the number after changeNum. Since changeNum only gets a copy of the number and not the number variable itself, the output of the program will still be the original variable which is 3.
That's for methods, for more examples look in Java examples in the home page.