คอนสตรัคเตอร์คล้ายกับเมธอดและถูกเรียกใช้ในเวลาที่สร้างอ็อบเจ็กต์ของคลาส โดยทั่วไปจะใช้เพื่อเริ่มต้นตัวแปรอินสแตนซ์ของคลาส ตัวสร้างมีชื่อเดียวกับคลาสและไม่มีประเภทส่งคืน
คอนสตรัคเตอร์มีสองประเภทคือคอนสตรัคเตอร์แบบกำหนดพารามิเตอร์และคอนสตรัคเตอร์แบบไม่มีอาร์กิวเมนต์
ตัวสร้างพารามิเตอร์
ตัวสร้างพารามิเตอร์ยอมรับพารามิเตอร์ซึ่งคุณสามารถเริ่มต้นตัวแปรอินสแตนซ์ได้ เมื่อใช้ตัวสร้างพารามิเตอร์ คุณสามารถเริ่มต้นตัวแปรคลาสแบบไดนามิกในขณะที่สร้างอินสแตนซ์ของคลาสด้วยค่าที่แตกต่างกัน
ตัวอย่าง
public class StudentData {
private String name;
private int age;
public StudentData(String name, int age){
this.name = name;
this.age = age;
}
public StudentData(){
this(null, 0);
}
public StudentData(String name) {
this(name, 0);
}
public StudentData(int age) {
this(null, age);
}
public void display(){
System.out.println("Name of the Student: "+this.name );
System.out.println("Age of the Student: "+this.age );
}
public static void main(String args[]) {
//Reading values from user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the student: ");
String name = sc.nextLine();
System.out.println("Enter the age of the student: ");
int age = sc.nextInt();
System.out.println(" ");
//Calling the constructor that accepts both values
System.out.println("Display method of constructor that accepts both values: ");
new StudentData(name, age).display();
System.out.println(" ");
//Calling the constructor that accepts name
System.out.println("Display method of constructor that accepts only name: ");
new StudentData(name).display();
System.out.println(" ");
//Calling the constructor that accepts age
System.out.println("Display method of constructor that accepts only age: ");
new StudentData(age).display();
System.out.println(" ");
//Calling the default constructor
System.out.println("Display method of default constructor: ");
new StudentData().display();
}
} ผลลัพธ์
Enter the name of the student: Krishna Enter the age of the student: 22 Display method of constructor that accepts both values: Name of the Student: Krishna Age of the Student: 22 Display method of constructor that accepts only name: Name of the Student: Krishna Age of the Student: 0 Display method of constructor that accepts only age: Name of the Student: null Age of the Student: 22