How much code should one put in a constructor?

前端 未结 9 2203
一整个雨季
一整个雨季 2020-12-15 05:34

I was thinking how much code one should put in constructors in Java? I mean, very often you make helper methods, which you invoke in a constructor, but sometimes there are s

9条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 06:05

    As little as is needed to complete the initialization of the object.

    If you can talk about a portion (5 or so lines is my guideline) of your constructor as a chunk of logic or a specific process, it's probably best to split it into a separate method for clarity and organizational purposes.

    But to each his own.

提交回复
热议问题