Skip to main content
4 events
when toggle format what by license comment
Jun 23, 2016 at 18:17 comment added qwertzguy I was about to post a similar answer, and I believe this is the right answer. So many people are blinded by those sayings that creating Java objects is cheap that they don't realize that often constructors or further initialization can be far from cheap. For example the ImageIcon class in Swing, even if you pass it an pre-loaded Image object, the constructor is pretty expensive. And I disagree with @GordonM, many classes from the JDK perform most of the init in the constructor, and I think it makes the code leaner, better designed.
May 24, 2012 at 14:19 comment added Alex If the additional cost is moved to the 'if (firstTime)', than recreating class in a loop is still more expensive than reusing the class.
May 24, 2012 at 6:00 comment added GordonM A well-designed class shouldn't do heavy-lifting in its constructor, for just this reason. For example, your file-reading class could lower its cost of instantiation by only verifying that its target file exists on startup, and deferring any actual file operations until the first method call that needs data from the file.
May 22, 2012 at 21:59 history answered Alex CC BY-SA 3.0