This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
It depends. The constructor can be used as a convenient way to set variables, or not prevent other classes from setting the value again via a setter. A problem arises when you have too many instance variables. Using a constructor to instantiate them all would look ugly and difficult to use. Also, using a constructor with parameters forces other classes to pass values when instantiating the class. This can be inconvenient, unless you have different kind of constructors, like a default constructor.
Christophe Verré wrote: A problem arises when you have too many instance variables. Using a constructor to instantiate them all would look ugly and difficult to use.
That can solved by implementing a builder.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Christophe Verré wrote: A problem arises when you have too many instance variables. Using a constructor to instantiate them all would look ugly and difficult to use.
That can solved by implementing a builder.
For the reference, that's recommended in Effective Java.