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.
public class Main {
static String greet(String name) {
return "Hello, " + name + "!";
}
public static void main(String[] args) {
System.out.println(greet("Chidi"));
}
}
Hello, Chidi!