Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 2
    -1 for "ctors should not do any calculation, just initializing the members," amongst others. Constructors oftentimes calculate things in order to initialize the members. The route this answer advocates is the route of getters and setters for every data member. Might as well make them all public, in that case. Commented Jan 2, 2018 at 0:55
  • 3
    -1 for the comment on constructor overloading. Many Java library classes have overloaded constructors. Commented Jan 2, 2018 at 1:44
  • @DavidHammen "The route this answer advocates is the route of getters and setters for every data member." No. The route this answer advocates is to calculate these values before passing them to the ctor. Commented Jan 2, 2018 at 11:07
  • @kevincline "Many Java library classes have overloaded constructors." As I wrote experienced programmers may find valid reasons to provide overloaded ctors. But this post is to an obvious novice. Commented Jan 2, 2018 at 11:10
  • @TimothyTruckle - if the calculation details are private to the implementation of the class, then clearly performing them before passing to the constructor will be a bit tricky (although, that said, I'm generally a fan of using a static method that returns an object for most non-trivial initialisation....) Commented Jan 3, 2018 at 5:50