Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!feeder.erje.net!newsfeed.utanet.at!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mail Newsgroups: comp.lang.java.programmer From: Andreas Leitgeb Subject: calling own methods from constructor Reply-To: avl@logic.at User-Agent: slrn/pre0.9.9-111 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: Date: 06 Apr 2011 20:48:41 GMT Lines: 26 NNTP-Posting-Host: gamma.logic.tuwien.ac.at X-Trace: 1302122921 tunews.univie.ac.at 60386 128.130.175.3 X-Complaints-To: abuse@tuwien.ac.at Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:2904 There is well-known danger in calling own methods from the constructor, namely that the method called may be overridden by a subclass, which is really instanciated, but whose specific constructor has not yet been run. I do not intend to delve into the details of static, private or final methods, or final'ity of the class itself (and maybe others) avoiding these problems, but instead I'm curious, why Java just doesn't simply forbid the dangerous calls. Is there any *good* use of having the constructor call a method that actually *can* be overridden in a subclass? I mean, are there (non-anti)patterns of explicitly allowing subclasses to hook into base-class's construction? --- sscce Test.java begin --- public class Test { Test() { foo(); } void foo() { } } --- sscce Test.java end --- PS: I know that this is not the only spot where Java does let one shoot in one's feet. But unlike other situations, this one just seems so easy to detect from static analysis.