Monday 29 April 2019

Why do we need default constructor? tccicomputercoaching.com

Constructor is used to initialize an object. In other words, constructor provides memory to an object. Without initializing an object we can’t use its properties.

But there is no need to define or declare a default constructor in Java . The compiler implicitly adds a default constructor in the program if we have not declared or defined it.



Example-

class Experiment
{
int a=10;
int b=20;
public static void main(String[] argus)
{
Experiment e=new Experiment(); //default constructor//
}
}

The only purpose of the constructor is to set a variable's default value, which will be zero anyway. This default constructor will be implicitly declared and created by the compiler.

For more information about constructor in Java,Constructor in C++, TCCI.

Call us @ 9825618292

Visit us @ www.tccicomputercoaching.com

No comments:

Post a Comment