0
What is the purpose of a default constructor?

Open 1 Answers 22 Views Education
What is the purpose of a default constructor?

1 Answer

0
What is the purpose of a default constructor?

The purpose of the default constructor is to assign the default value to the objects. The java compiler creates a default constructor implicitly if there is no constructor in the class.

class Student3{  

int id;  

String name;  

  

void display(){System.out.println(id+" "+name);}  

  

public static void main(String args[]){  

Student3 s1=new Student3();  

Student3 s2=new Student3();  

s1.display();  

s2.display();  

}  

}  

Test it Now

Output:

0 null

0 null

Explanation: In the above class, you are not creating any constructor, so compiler provides you a default constructor. Here 0 and null values are provided by default constructor.

Java default constructor
by (52.6k points)
...