ทุกออบเจ็กต์ของคลาสถูกสร้างโดยใช้คีย์เวิร์ด new เดียวกัน ดังนั้นจึงต้องมีข้อมูลเกี่ยวกับคลาสที่จะต้องสร้างออบเจ็กต์ เพราะฉะนั้น ชื่อคอนสตรัคเตอร์จึงควรจะเหมือนกับชื่อคลาส
ตัวอย่าง
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