Initializing final fields in Java

后端 未结 4 1871
生来不讨喜
生来不讨喜 2021-02-07 10:12

I have a class with lots of final members which can be instantiated using one of two constructors. The constructors share some code, which is stored in a third constructor.

4条回答
  •  情话喂你
    2021-02-07 11:10

    All you need to do is ensure that "two" gets initialized. In the first constructor, just add:

    two = null;
    

    unless there's some other value you'd like to give it in the event that only the first constructor is called.

提交回复
热议问题