Can you have logic in a constructor?

Can you have logic in a constructor?

Yes. With a sufficiently large input, you can cause an exception in your constructor. This is one of several reasons why you should not use a constructor to contain “business logic”.

How big should a constructor be?

If you can talk about a portion (5 or so lines is my guideline) of your constructor as a chunk of logic or a specific process, it’s probably best to split it into a separate method for clarity and organizational purposes. But to each his own. Constructors should be just long enough, but no longer =)

Can you use this () and super () both in a constructor justify the reason with example?

both this() and super() can not be used together in constructor. this() is used to call default constructor of same class.it should be first statement inside constructor. super() is used to call default constructor of base class.it should be first statement inside constructor.

Do you want the constructor to do any computation?

You usually do not want the constructor to do any computation. Someone else using the code will not expect that it does more than basic set-up. For a directory tree like you’re talking about, the “elegant” solution is probably not to build a full tree when the object is constructed. Instead, build it on demand.

What can an unlicensed contractor do for You?

That allows building officials to visit the site periodically to confirm that the work meets safety codes. On small interior jobs, an unlicensed contractor may try to skirt the rule by telling you that authorities won’t notice.

Where can I find out if my contractor is telling the truth?

If you’re unsure whether your contractor is telling the truth about structural problems, you can get an impartial opinion from a home inspector, the local branch of the National Association of Home Builders, or even your local building department.

When do you need to construct an object in a constructor?

Should operations that could take some time be performed in a constructor or should the object be constructed and then initialised later. For example when constructing an object that represents a directory structure should the population of the object and its children be done in the constructor.