init method vs constructors

I usually start with and stick to constructors, and avoid creating init() until i see justifications such as
* multiple constructors need to share some initialization code. You can have init1(…), init2(..) as lego’s to be used by these constructors.

* init(..) can have weird arguments, unsuitable for constructors.
* constructors constraints — must call super(…) as*first* statement
* Spring init-method won’t work with constructors??

* a special justification in a multi-threaded context
All my constructors try to quickly button up the object and return. Before a constructor returns, the object is invalid. If a constructor creates a thread that thread might access this invalid object. You can put crazy things (DB, network access…) into init(), and you can choose to call init() after (or before?) a constructor returns.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s