Methods
A method has a return type, a name and parameters. Use void when nothing is returned. Methods in the same class that are called from main must be static. Call a method by its name with arguments.
Example
public class Main {
static void greet(String name) {
System.out.println("Hello, " + name);
}
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
greet("Ada");
System.out.println(add(2, 3));
}
}Result
Hello, Ada 5
Exercise
A method that returns nothing uses...
static greet() {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.