Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much code should one put in a constructor?

Tags:

I was thinking how much code one should put in constructors in Java? I mean, very often you make helper methods, which you invoke in a constructor, but sometimes there are some longer initialization things, for example for a program, which reads from a file, or user interfaces, or other programs, in which you don't initialize only the instance variables, in which the constructor may get longer (if you don't use helper methods). I have something in mind that the constructors should generally be short and concise, shouldn't they? Are there exceptions to this?

like image 282
user42155 Avatar asked Feb 10 '09 06:02

user42155


Video Answer


1 Answers

If you go by the SOLID principles, each class should have one reason to change (i.e. do one thing). Therefore a constructor would normally not be reading a file, but you would have a separate class that builds the objects from the file.

like image 84
krosenvold Avatar answered Nov 02 '22 20:11

krosenvold