Method (Java)

A method is a reusable, named block of code that performs a task. It belongs to a class, can take inputs called parameters, and can return a value.

Java

A method's signature includes its return type (or void if it returns nothing), its name, and its parameter list. You call a method by its name, passing any required arguments.

Methods help you avoid repeating code and break a program into smaller, organized pieces. Method names use lowerCamelCase by convention.

Examples

Java

public class Main {
    static String greet(String name) {
        return "Hi, " + name;
    }

    public static void main(String[] args) {
        System.out.println(greet("Lee"));
    }
}

Output:

Hi, Lee

Learn it in a lesson

See also

Back to the Dictionary