برای فراخوانی یک متد در جاوا، نام متد را بعد از دو بنویسید
پرانتز () و نقطه ویرگول;
در مثال زیر، myMethod() برای چاپ یک متن (عملی) استفاده میشود، زمانی که آن را فراخوانی کنید:
مثال
Inside main, call the
myMethod() method:
public class MyClass {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
}
}
// Outputs "I just got executed!"
یک روش را میتوان چندین بار نیز فراخوانی کرد:
مثال
public class MyClass {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
myMethod();
myMethod();
}
}
// I just got executed!
// I just got executed!
// I just got executed!
در فصل بعدی، پارامترهای روش، نحوه انتقال داده ها (پارامترها) به یک متد را خواهید آموخت.