Is it violation of Clean Code to call init method in constructor like this

后端 未结 6 2167
自闭症患者
自闭症患者 2020-12-14 21:05

My concern in the code below is that the param to constructor is not actually directly mapped to the class\'s instance fields. The instance fields derive value from the para

6条回答
  •  不思量自难忘°
    2020-12-14 21:25

    You should never call non-final methods in a constructor. Effective Java does a good job explaining why, but basically your object is not in a stable state before the constructor returns. If your constructor calls methods which are overridden by a subclass, you can get strange, undefined behavior.

    Also see this answer.

提交回复
热议问题