Method Definition
A method is a section of code inside a class that allows us to tell the code to do something.
There are several parts to a method. The ones we will focus on now are the method name and method body.
A method has a name, which you use in your program to execute statements. * A method name is always followed by a pair of parentheses.
The method body is a set of curly braces containing the statements to execute.
public static void methodName() {
// this is a method body
// statements to execute...
}
All methods are defined inside a class's curly braces.
public class MyClass {
public static void methodName() {
// this is a method body
// statements to execute...
}
// ...
}
The class below declares a method named printManyAsterisks.
public class PrintAsterisks {
public static void printManyAsterisks() {
System.out.println("****************");
System.out.println("********");
System.out.println("***");
}
}
declare¶
Writing the method in your class, giving it a name and a method body.
When a program uses, or calls the printManyAsterisks method, three lines of * are printed to the screen.
call¶
Using the method in your class, referring to it by its name.