Does it affect the time in loading the application?
or any other issues in doing so?
Does it affect the time in loading the application? or any other issues in
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The question is vague on what “long” means. Here are some possible interpretations:
Interpretation #1: The constructor has many parameters
Constructors with many parameters can lead to poor readability, and better alternatives exist.
Here’s a quote from Effective Java 2nd Edition, Item 2: Consider a builder pattern when faced with many constructor parameters:
The telescoping constructor pattern is essentially something like this:
And now you can do any of the following:
You can’t, however, currently set only the
nameandisAdjustable, and leavinglevelsat default. You can provide more constructor overloads, but obviously the number would explode as the number of parameters grow, and you may even have multiplebooleanandintarguments, which would really make a mess out of things.As you can see, this isn’t a pleasant pattern to write, and even less pleasant to use (What does “true” mean here? What’s 13?).
Bloch recommends using a builder pattern, which would allow you to write something like this instead:
Note that now the parameters are named, and you can set them in any order you want, and you can skip the ones that you want to keep at default values. This is certainly much better than telescoping constructors, especially when there’s a huge number of parameters that belong to many of the same types.
See also
Related questions
Interpretation #2: The constructor does a lot of work that costs time
If the work must be done at construction time, then doing it in the constructor or in a helper method doesn’t really make too much of a difference. When a constructor delegates work to a helper method, however, make sure that it’s not overridable, because that could lead to a lot of problems.
Here’s some quote from Effective Java 2nd Edition, Item 17: Design and document for inheritance, or else prohibit it:
Here’s an example to illustrate:
Here, when
Baseconstructor callsoverrideMe,Childhas not finished initializing thefinal int x, and the method gets the wrong value. This will almost certainly lead to bugs and errors.Interpretation #3: The constructor does a lot of work that can be deferred
The construction of an object can be made faster when some work is deferred to when it’s actually needed; this is called lazy initialization. As an example, when a
Stringis constructed, it does not actually compute its hash code. It only does it when the hash code is first required, and then it will cache it (since strings are immutable, this value will not change).However, consider Effective Java 2nd Edition, Item 71: Use lazy initialization judiciously. Lazy initialization can lead to subtle bugs, and don’t always yield improved performance that justifies the added complexity. Do not prematurely optimize.