ทุกอ็อบเจ็กต์ของคลาสถูกสร้างขึ้นโดยใช้คีย์เวิร์ดใหม่เดียวกัน ดังนั้นต้องมีข้อมูลเกี่ยวกับคลาสที่อ็อบเจ็กต์จะต้องสร้างออบเจกต์ ด้วยเหตุนี้ ชื่อตัวสร้างควรเหมือนกับชื่อคลาส
ตัวอย่าง
class MyConstructor{
public MyConstructor() {
System.out.println("The constructor name should be same as the class name");
}
public static void main(String args[]){
MyConstructor mc = new MyConstructor();
}
} ในโปรแกรมข้างต้น ชื่อคอนสตรัคเตอร์ควรเหมือนกับชื่อคลาส (MyConstructor)
ผลลัพธ์
The constructor name should be same as the class name