What is the purpose of a default constructor in Java?

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.

  1. class Student3{
  2. int id;
  3. String name;
  4. void display(){System.out.println(id+” “+name);}
  5. public static void main(String args[]){
  6. Student3 s1=new Student3();
  7. Student3 s2=new Student3();
  8. s1.display();
  9. s2.display();
  10. }
  11. }


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.