method adalah suatu kumpulan kode yang akan dijalankan ketika dipanggil.
Kita dapat memasukkan data ke dalam method.
Dengan menggunakan method, kita cukup mendefinisikan kode sekali dan dapat digunakan berulang kali.
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
Why use methods?
To reuse code: define the code once, and use it many times.
Create a method inside Main:
public class Main {
static void myMethod() {
// code to be executed
}
}
public class Main {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
myMethod();
myMethod();
}
}