[Let's learn something new] Explicit constructor invocation
JavaShortSeries
5 December, 2023
52
52
0
Contributors
- I learnt something new today and will be sharing it w everyone.
- We might have seen general uses of
this
keyword to set the field values of object that is being called, but today we're about to see something interesting, the usage ofthis
keyword to call a constructor within another constructor. - This is called
explicit constructor invocation
- In this example, the constructor with no arguments calls the constructor with four arguments and thereby setting those
fields
with the given values.
- Note: This explicit invocation must be done the first line in the respective constructor body.
- source:
[From the official Java docs] https://dev.java/learn/classes-objects/more-on-classes/