AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
corejava-OOPs: What is the purpose of default constructor ?
As If there is no constructor in a class, compiler automatically creates a default constructor. and it doesn´t provide any value. so what´s the use of default constructor.
corejava x 353
OOPs x 49
Posted On :
2013-11-19 23:24:29.0
Garima Gupta
596
1295
60202
3
Answers
Constructor is a special type of method that is used to initialize the object. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
Default constructor provides the default values to the object like 0, null etc. depending on the type.
Let´s see the following example :
class Student{
int id;
String name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.display();
s2.display();
}
}
It´ll give following output :
0 null
0 null
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.
Posted On :
2013-11-19 23:35:53
Satisfied :
2 Yes
0 No
Rishi Kumar
523
1882
50042
Reply This Thread
4
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in