Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default constructor in java when variables are already set to default by java

We know the default value of primitive data types is provided by java e.g: for int we have 0. We have a default constructor in java which also does the same job. What's the need for that? The state of any object would be the same by default if java did not have the default constructor.

I am not asking for an answer with respect to beans but for the sole purpose i.e. initialization. Why do we have a default constructor?

like image 285
FaisalHBTI Avatar asked Nov 23 '25 17:11

FaisalHBTI


1 Answers

Its not only about only primitive datatype initialization but the initialization of class member datatype.

For example: when you are creating object of a class and not defining default constructor and any parameterised constructor , then JVM will add default constructor which will have call to constructor of its base class just to make sure all the base class member variables are initialized.

Also please check this answer for more details.

like image 54
Vishrant Avatar answered Nov 25 '25 07:11

Vishrant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!