Uncategorized

Lesson 7 – Methods in Java

 

If you have a large piece of code that you want to execute repeatedly, the preferred way to do so is via methods. Methods are also known as functions. A method has two parts: method definition and method call. The method definition consists of code block. Once the method is defined you can call the method to execute the code that the method body contains.

A Simple Method

Let’s see a very simple example of a method.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub


print_hello()

}

public static void print_hello()
{
System.out.println("Hello, this is a method.");
}
}

In the above code, we have two methods. One is the “main” method which is the starting point of the code. The second is the “print_hello” method. To define a method, you have to specify the access modifier which can be public, private or protected. A public  method is accessible everywhere. A private method is only accessible within a class. A protected method is accessible inside a parent and its child classes. We will see objects and classes in the next lesson. The word “static” in the method definition specifies that the method can be called without class object. The keyword “void” means that the method doesn’t return anything. The code block for the method is specified inside opening and closing braces. The call the to a method is simply the method name followed by opening and closing parenthesis. Inside the main method “print_hello()” is the call to “print_hello” method. When you execute the above code, the main method calls the “print_hello” method and the code inside the “print_hello” method executes. Now whenever you want to execute the code block for the “print_hello” method, simply call the method. The output of the above script is as follows:

Hello, this is a method.

Passing Values to a Method

You can also pass values to methods. In the method definition, inside the opening and closing brackets that follow the method name, you have to specify the type of values that the method will accept. While calling the function, you pass the value to the method via parenthesis. Look at the following example:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

print_message("Hello from the calling function");
}

public static void print_message(String message)
{
System.out.println(message);
} 
}

In the above code, we have a function “print_message” which accepts one string value from the function call. In the function call, we pass a message to the “print_message” function which prints the message passed to it on the console. The output looks like this:

Hello from the calling function

Returning Values from Methods

A method can also return values to the calling function. The value is returned via the “return” keyword. The “return_square” method in the following code accepts an integer value and returns its square.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int result = return_square(8);
System.out.println("Square of 8 is: "+ result);
}

public static int return_square(int n)
{
System.out.println("This method returns square.");
return (n * n);
}

}

Here is the output:

This method returns square.
Square of 8 is: 64

Let’s see another example of return square.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int result = add_and_return(8,10);
System.out.println("Square of 8 and 10 is : "+ result);
}
public static int add_and_return(int a, int b)
{
System.out.println("This method returns sum of two numbers.");
return (a + b);
}

}

In the above code, the “add_and_return” method return the sum of two numbers passed to it as parameters. The output looks like this:

This method returns sum of two numbers.
Square of 8 and 10 is : 18

What’s Next

In this lesson, you studied Methods in Java. In the next lesson, you will studies classes and objects.