Java Methods

Summary: in this tutorial, you will learn about Java methods and how to use them to organize your code effectively.

Introduction to Java methods

In Java, a method is a named code block within a class that performs a specific task. A method allows you to encapsulate a block of code, making it more manageable and reusable.

For example, you may need to write a piece of code that performs the same task in different places and you don’t want to copy that code all over the program. To do that, you can use a method to wrap the code and use the method instead.

In practice, you often use methods to divide a large code block into smaller and more manageable ones.

Declaring a method

In Java, you declare a method within a class. Here’s the basic syntax of declaring a method:

accessModifier returnType methodName(parameters) {
    // Code to perform a specific task
}Code language: Java (java)

In this syntax:

  • Access Modifier: Determine the visibility of the method. In other words, it defines which part of the program can call the method. Common access modifiers are public, private, and protected. The access modifier is optional. If you omit the access modifier, the method will be package-private.
  • Return Type: Specifies the type of value that the method will return. If the method doesn’t return any value, its return type is void. To return a value from the method, you use the return statement.
  • Method Name: The name of the method used to identify the method. By convention, the method name follows the camel case e.g., add, addOne.
  • Parameter List: Contains zero or more parameters enclosed in the parentheses. Parameters are the input of the method.

Calling a method

To use a method within the class, you need to call it. You call a method by its name followed by parentheses, and pass arguments if the method has the parameters like this:

methodName(arguments);Code language: JavaScript (javascript)

To call a method explicitly within the class, you can use the this keyword:

this.methodName(arguments);Code language: JavaScript (javascript)

To call a method from the outside of the class, you create an instance of the class and invoke the method via the object of the class:

var myObject = new ClassName();
myObject.methodName(arguments);Code language: JavaScript (javascript)

If the method is static, you can call the method via the class name:

ClassName.methodName(arguments);Code language: JavaScript (javascript)

Examples of Java methods

Let’s take some examples of declaring and calling Java methods.

1) Defining Java methods that have no parameters and return value

First, define a simple method that displays the message Hi on the screen:

public static void greeting() {
    System.out.println("Hi");
}Code language: Java (java)

Second, call the greeting() method:

greeting();Code language: Java (java)

Here’s the complete program:

public class App {
    public static void greeting(){
        System.out.println("Hi");
    }

    public static void main(String[] args){
        greeting();
    }
}Code language: Java (java)

2) Defining Java methods that have no parameters and a return value

To return a value in the method, you use the return statement:

return value;Code language: Java (java)

When the return statement is executed, it terminates the method and returns the result to the caller.

First, redefine the greeting() method that returns a String:

public static String greeting() {
    return "Hi";
}Code language: Java (java)

Second call the greeting() method, assign its return value to a variable, and display the return value:

var message = greeting();
System.out.println(message);Code language: Java (java)

Here’s the complete program:

public class App {
    public static String greeting(){
        return "Hi";
    }

    public static void main(String[] args){
        var message = greeting();
        System.out.println(message);
    }
}Code language: Java (java)

3) Defining Java methods that have parameters and return a value

A method may have one or more parameters. They are like local variables that are available only inside the method.

For example, the following greeting() method has a name parameter as a String and returns a string:

public static String greeting(String name) {
    return "Hi" + name;
}Code language: Java (java)

Because the greeting() method expects a parameter, you need to pass an argument when calling it. For example:

var message = greeting("John");
System.out.println(message);Code language: Java (java)

In this example, the name is called a parameter while the literal string "John" is called an argument.

In short, parameters are what you define in the method, while the arguments are what you pass to the method. Sometimes, you’ll find that the terms are used interchangeably.

Here’s the complete program:

public class App {
    public static String greeting(String name){
        return "Hi" + name;
    }

    public static void main(String[] args){
        var message = greeting("John");
        System.out.println(message);
    }
}Code language: Java (java)

Summary

  • Use Java methods to structure your code logically and make it reusable.
  • Use the return statement to return a value from a method.
  • Use void if a method doesn’t return any value.
Was this tutorial helpful ?