What is the purpose of a default constructor?

Java Programming Language >   Class, Object and Methods in java >   Constructor  

Short Question

1300


Answer:

The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.


Example of default constructor that displays the default values

//Let us see another example of default constructor  
//which displays the default values  
class Student3{  
int id;  
String name;  
//method to display the value of id and name  
void display(){System.out.println(id+" "+name);}  
  
public static void main(String args[]){  
//creating objects  
Student3 s1=new Student3();  
Student3 s2=new Student3();  
//displaying values of the object  
s1.display();  
s2.display();  
}  
}  
0 null
0 null


This Particular section is dedicated to Question & Answer only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.




Join Our telegram group to ask Questions

Click below button to join our groups.