ใช่ , อนุญาตให้กำหนด วิธีการที่มีชื่อเดียวกัน เป็นของชั้นเรียน ไม่มีการคอมไพล์เวลาหรือข้อผิดพลาดรันไทม์ที่จะเกิดขึ้น แต่ไม่แนะนำตามมาตรฐานการเข้ารหัสใน Java โดยปกติชื่อคอนสตรัคเตอร์และชื่อคลาสจะเหมือนกันเสมอ ใน Java
ตัวอย่าง
public class MethodNameTest {
private String str = "Welcome to TutorialsPoint";
public void MethodNameTest() { // Declared method name same as the class name
System.out.println("Both method name and class name are the same");
}
public static void main(String args[]) {
MethodNameTest test = new MethodNameTest();
System.out.println(test.str);
System.out.println(test.MethodNameTest());
}
} ในตัวอย่างข้างต้น เราสามารถประกาศชื่อเมธอด (MethodNameTest ) เหมือนกับชื่อคลาส (MethodNameTest ) จะถูกคอมไพล์สำเร็จโดยไม่มีข้อผิดพลาด
ผลลัพธ์
Welcome to TutorialsPoint Both method name and class name are the same