Best way to handle multiple constructors in Java

后端 未结 9 1461
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 07:00

I\'ve been wondering what the best (i.e. cleanest/safest/most efficient) way of handling multiple constructors in Java is? Especially when in one or more constructors not al

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 07:44

    Another consideration, if a field is required or has a limited range, perform the check in the constructor:

    public Book(String title)
    {
        if (title==null)
            throw new IllegalArgumentException("title can't be null");
        this.title = title;
    }
    

提交回复
热议问题