Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correlation between constructors in Java and __init__ function in Python

I was currently learning more about constructors in Java, and I found out that just like the __init__ function in Python, constructors are functions that are called as soon as we instantiate an object of a class.

So, are both the concepts one and the same, abstractly?

like image 890
Manas Chaturvedi Avatar asked May 17 '26 14:05

Manas Chaturvedi


1 Answers

These are very similar things, however with at least one big difference.

  • constructor is called before/while the object is being constructed
  • __init__ is called after the object has been constructed, so you have a valid reference to it (called self)
like image 147
lejlot Avatar answered May 20 '26 03:05

lejlot



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!