0 votes
12 views
What is the purpose of a default constructor?
in Education by (48.2k points)

1 Answer

0 votes
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 (48.2k points)

Related questions

0 votes
1 answer 13 views
0 votes
1 answer 13 views
0 votes
1 answer 26 views
0 votes
1 answer 14 views
0 votes
1 answer 9 views
9 views asked Nov 17, 2020 in Education by Editorial Staff (48.2k points)
0 votes
1 answer 16 views
0 votes
1 answer 12 views
0 votes
1 answer 13 views
0 votes
1 answer 45 views
0 votes
1 answer 12 views
0 votes
1 answer 33 views
0 votes
1 answer 12 views
0 votes
1 answer 19 views
0 votes
1 answer 15 views
0 votes
1 answer 18 views
0 votes
1 answer 14 views
0 votes
1 answer 14 views
0 votes
1 answer 13 views
0 votes
1 answer 12 views
...