Is it better to have constructors with or without parameters?

Is it better to have constructors with or without parameters and why?

public NewClass( String a,  String b, int c) throws IOException
{
    //something
}

OR

public NewClass() 
{
    //something
}

12

A constructor should establish the initial invariant of your object, that is, put it in a valid and usable state.

If your object is not really usable as an instance of the type it is after construction, it’s a sign that you’ve got a bit of a smear between initialization of the object and use of the object.

If it’s impossible to provide all the information needed up-front to construct your object properly, you may want to consider some sort of builder to gather state incrementally before instantiating the object.

In general, zombie-type objects which have initialization after construction and invalidation before disposal tend to be error-prone, particularly if there is no language support for it, leaving you to enforce the concepts in documentation and assertions.

2

I prefer constructors with parameters and there are two main reasons:

  • Passing values into a constructor means that the fields can be final and therefore validated and set one time only. Note that final fields make multi-threaded coding much simpler (see http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601).
  • Makes testing much easier if the values (especially objects) passed via a constructor can be mocked to behave in a desired way. Helps testing in isolation.

I don’t think there is anything wrong with getter setters, but I typically only use them in a ‘builder’ or ‘bean’ class.

0

You should favor parameterless constructors.

There’s two main reasons for this, the first being that constructors have no good way to report errors. Sure, you can toss exceptions but that’s a fairly high impact response to simple bad data – it forces the users of your object to treat it with undue care since any creation can throw.

The second is that it can sometimes (in some languages especially) cause disconnects (or duplicated code) between the validation for constructor setting and property setting to enforce your invariance. Since the class invariants are the same, the validation should be the same. By allowing two effective ways to set values, it complicates the class’ validation.

To be clear, I’m not saying to never have parameterized constructors. And I’m definitely not saying to have some Initialization method. What I’m saying is that objects should have a clear, well-defined initial state with sane defaults.

Sometimes objects can’t have sane defaults and instead need their initial state passed to them. It happens. But in general, you should look to avoid it.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật