Java Methods with Examples

A method is a set of code written to do a specific task or operation in a program. Methods are also called functions.

A method runs only when it is called.

You can pass values inside a method via parameters.

Why use methods in Java?

The use of methods make the code more readable and reusable. With the use of methods, you can write code once and run it many times.

Create Methods in Java

In Java, methods are created inside a class. A class may have any number of methods. To create a method, the first thing you'll need to do is, define the access modifier, followed by the return type of the method, the name of the method, the parentheses ( ), and the body of the method enclosed by open and close curly braces { }. Inside the body is where all the logic of the code to perform some tasks are written.

Example

Here is an example of a method that does not return a value when it runs:


public class MyExample {

    public void myTask() {
        // task code here
    }
}

Here is an explaination of the above public void myTask() method:

  • public is the access modifier.
  • void indicates that this method does not return any value.
  • myTask is the name of the method.
Example

Here is another example of a method that returns a value when it runs:


public class MyCalculator {

    public int add(int num1, int num2) {

        int result = num1 + num2;
        return result;
    }
}

Here is an explaination of the above public int add(int num1, int num2) method:

  • public is the access modifier.
  • int indicates that this method returns an integer value.
  • add is the name of the method.
  • (int num1, int num2) are parameters via which data can be passed into the method.
  • return is the keyword which is used to return a value.

Call a Method in Java

To call a method within the same class where the method is created in Java, simply write the method's name followed by parentheses () and a semicolon ;.

Example

Here is an example of calling methods within another method of the same class:


public class Person {

    private String name;
    private String email;

    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void printPerson() {

        setName("Danny Kent");
        setEmail("danny@tutorialsbuddy.com");
    }
}

To call a method of one class in a different class, you'll first need to create an object of the class that contains the method and calls the method on the object using the period or dot . operator.

Example

Lets create a Calculator class with four methods which returns values:


public class Calculator {

    public int add(int num1, int num2) {
        int result = num1 + num2;
        return result;
    }

    public int substract(int num1, int num2) {
        int result = num1 - num2;
        return result;
    }

    public double multiply(double num1, double num2) {
        double result = num1 * num2;
        return result;
    }

    public double divide(double divident, double divisor) {
        double result = divident / divisor;
        return result;
    }

}

Now, here is an example of calling the methods of the Calculator class in a different Store class:


`public class Store {

    public static void main(String[] args) {

        Calculator calculator = new Calculator();
        int addResult = calculator.add(5, 10);
        int substractResult = calculator.substract(10, 2);
        double multiplyResult = calculator.multiply(5, 10);
        double divideResult = calculator.divide(1407, 2);

        System.out.println(addResult);
        System.out.println(substractResult);
        System.out.println(multiplyResult);
        System.out.println(divideResult);
    }
}
Output
15
8
50.0
703.5

Static Methods in Java

In Java, a static method is a method that belongs to a class and not to an instance/object of a class.

A static method in Java, is created using the static keyword right after the declaration of the access modifier. The static keyword can also be declared before the access modifier but it is not recommended because doing so does not comply with the Java language specification.

A static method can be called without creating an object of a class. To call a static method, you simply need to write the Class name followed by a dot . without space and the method name with parentheses and a semicolon ; .

Example

Lets create a Calculator class with four static methods:


public class Calculator {

    public static int add(int num1, int num2) {
        int result = num1 + num2;
        return result;
    }

    public static int substract(int num1, int num2) {
        int result = num1 - num2;
        return result;
    }

    public static double multiply(double num1, double num2) {
        double result = num1 * num2;
        return result;
    }

    public static double divide(double divident, double divisor) {
        double result = divident / divisor;
        return result;
    }

}

Here is an example of calling static methods:


public class Store {

    public static void main(String[] args) {


        int addResult = Calculator.add(5, 10);
        int substractResult = Calculator.substract(10, 2);
        double multiplyResult = Calculator.multiply(5, 10);
        double divideResult = Calculator.divide(1407, 2);

        System.out.println(addResult);
        System.out.println(substractResult);
        System.out.println(multiplyResult);
        System.out.println(divideResult);
    }
}
Output
15
8
50.0
703.5

When to use static Methods in Java

Use static methods whenever there is no need of creating an instance of a class to use those methods. In the real world programs, static methods are generally used in Utility classes. Utility classes are stateless and cannot be instantiated. Utility classes usually contains common, often reused methods.