Call methods

After defining a method in Java, it can be called using the method name.

Code Example

class MethodCall {

	private static void print() {
		System.out.println("Test method");
	}

	public static void main(String[] args) {
		print();
		print();
	}
}
Output
Test method
Test method
Code Explanation

This is a Java program that defines a class called MethodCall. The class contains a private static method print() that simply prints the string “Test method” to the console.

The main method of the class calls the print() method twice, so when the program is run, it will print “Test method” to the console twice.