In Java, you call a method by specifying the method name followed by parentheses () and any required parameters inside the parentheses
public class Main {
public static void main(String[] args) {
sayHello(); // Method call without parameters
}
public static void sayHello() {
System.out.println("Hello, World!");
}
}
In this example, sayHello is a method defined in the Main class. It's called within the main method by writing sayHello().
If a method requires parameters, you would include the values for those parameters inside the parentheses during the call, like so:
public class Main {
public static void main(String[] args) {
greet("John"); // Method call with parameter
}
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
In this second example, the greet method is called with the argument "John" which is passed as a parameter to the method.