Is there any purpose for declaring an init()
method for a type?
I'm not asking whether we should prefer init()
over a constructor or how to avoid declaring init()
.
I'm asking if there is any rationale behind declaring an init()
method (seeing how common it is) or if it's a code smell and should be avoided.
The init()
idiom is quite common, but I have yet to see any real benefit.
I'm talking about types that encourage initialization via a method:
class Demo {
public void init() {
//...
}
}
When will this ever be of use in production code?
I feel it may be a code smell since it suggests the constructor does not fully initialize the object, resulting in a partially created object. The object should not exist if it's state isn't set.
This makes me believe it may be part of some kind of technique used to speed up production, in the sense of enterprise applications. It is the only logical reason I can think of having such an idiom, I'm just not sure how it would be beneficial if so.
init()
on the derived class, or vice versa?) If so, it is an example of letting base class execute a "post-constructor" which can only be executed after the most derived class has finished construction. It is an example of multi-phase initialization.