← Java tutorial

Java

Methods

A method's signature declares what type it returns (or void for nothing), its name, and its parameters, each with its own declared type.

Example: a static method and calling it

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

    public static void main(String[] args) {
        System.out.println(greet("Chidi"));
    }
}
Hello, Chidi!

Java calls these methods rather than "functions" specifically because they live inside a class, a bare, standalone function outside any class doesn't exist in Java the way it does in Python or JavaScript. The static keyword here means this particular method belongs to the class itself, not to any specific object instance of it, you'll meet the distinction properly in the Classes & Objects lesson.

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

    public static void main(String[] args) {
        System.out.println(greet("Chidi"));
    }
}
Output
Hello, Chidi!
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Active

Enroll in Backend Development
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account