Back to Top
 
 

Methods

Methods are defined sets of instructions. They have many advantages: one of the most important ones is that they in a clear way order the actions that the program performs. They allow certain functionalities to be used in different parts of the program effectively.

In fact we have been using one of them even before we started talking about methods. It was the 'main' method: a starting method of a program. Our programs can have many methods but without the 'main' one we will not be able to run them.

The syntax of the 'main' method is quite complex, it contains the words: 'public' and 'static' that we don't need to know yet. For now, let's assume that all of our methods are going to start from 'public' and 'static'.

Method declaration

If we want to write our own method we do it under the 'main' one. At the beginning let's learn basic declaration of methods. This is done according to the pattern:

returned_type method_name(arguments);

Now, what is this 'returned_type'?

Well, in this place we put the information on the type of value that the method must return after execution. If we want the execution results to be a number, we declare 'int'. If it is to be text: we write 'String', etc. And if we don't want the method to return anything we use the word 'void' in this place.

Method arguments

A method can take arguments: we write them inside parentheses, immediately after the name of the method.

But arguments are not a required element: if we don't want the method to take arguments, we leave the parentheses empty. There can be any number of arguments and they can have different types: we divide them with commas.

See an example of method creation:

Notice that apart from declaring our methods we also need to call them in the starting method of our program, the 'main' method. Calling the method means writing its name and adding any arguments it has.

If we want to display the result of the method that returned something we do it with the instruction we already know System.out.println() and put the call to the method inside the parentheses.

Discussing and calling methods

Method 1

 

We declared a method that should return a number (int). The 'return' instruction tells us it is going to be 5:

public static int returnNumber(){
 return 5;
 } 

Call: System.out.println(returnNumber());

Method 2

 

In this example we declare returning text (String type) and in the 'return' instruction we can see that it will be the sentence: "The method returns text".

public static String returnString() {
 return "The method returns text.";
 }

Call: System.out.println(returnString());

Method 3

 

The following method, just like the previous one returns text but additionally takes a String type argument that we put inside parentheses behind the name of the method.

Here, the method result depends on the value of the argument that is passed when calling the method. In the example we have the name "Jola" so in this case the method is going to return "Hello Jola".

public static String sayHello(String name){
 return "Hello " + name;
 }

Call: System.out.println(sayHello("Jola"));

Method 4

 

This method has the word 'void' instead of the declared type. That's how we know that it will execute the instruction in the body but not return anything. Notice that there's no 'return' in this method.

public static void printText(){
 System.out.println("The method prints text,
but it does not return anything.");
 }

Call: printText();

Method 5

 

This method declares that it returns an int type value and takes two arguments. It performs addition in the body and returns the operation result.

public static int sum(int a, int b) {
 int sum = a + b;
 return sum;
 }

Call: System.out.println(sum(3,5));
 

Tasks

 

Task 1

Write two methods: one taking an int type argument and another one: a String. The first method should return your current age, the second one your name. Print the results of both methods to the screen.

Go to the first task:

Task 2

Write a method that takes one int type argument and raises it to the second power. In Java there is a special function used for exponentiation but for now it's okay if you multiply the number by itself.

Go to the second task:

Task 3

Write a method that takes int type parameters. Each of the parameters is going to mean the age of someone in your immediate family: there will be as many parameters as people in your family. The method should not return anything, just sum up your ages and print it to the screen.

Go to the third task: