DEV Community

loading...

Constructor Chaining In Java with Examples

skptricks profile image skptricks ・1 min read

Post Link : Constructor Chaining In Java with Examples

Calling a constructor from the another constructor of same class is known as Constructor chaining. The real purpose of Constructor Chaining is that you can pass parameters through a bunch of different constructors, but only have the initialization done in a single place. This allows you to maintain your initialization from a single location, while providing multiple constructors to the user. If we don’t chain, and two different constructors require a specific parameter, you will have to initialize that parameter twice, and when the initialization changes, you’ll have to change it in every constructor, instead of just the one.

Click to view example

Discussion (1)

pic
Editor guide
Collapse
gregorys100 profile image
Gregory Sequeira

So one major benefit of this method is that you can avoid null values when accessing that object.

Good stuff!