Qus:    What is the purpose of a default constructor?
Aug 18, 2022 16:37 Java 1 Answers Views: 1103 Tweat heigh
Prev Next
Answers (1)
IMMEDIATE EDGE Feb 15, 2021 15:03
Answer:   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.
5. void display(){System.out.println(id+" "+name);}
6.
7. public static void main(String args[]){
8. Student3 s1=new Student3();
9. Student3 s2=new Student3();
10. s1.display();
11. s2.display();
12. }
13. }
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.

Post Your Answer
Guest User

Not sure what solution is right for you?

Choose the right one for you.
Get the help of the experts and find a solution that best suits your needs.


Let`s Connect