Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.help Subject: Re: Why can one invoke setters and getters from a (class) constructor? Date: Mon, 25 Apr 2011 15:14:03 -0400 Organization: albasani.net Lines: 24 Message-ID: References: <87bozwkgz8.fsf@merciadriluca-station.MERCIADRILUCA> <0na8r6tg38fu60ojepfl5h4u20lmhfhffr@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net o3EbpQIPbdoiZYisH15ssf5GTGXV4qMQvxamzCV8XWuWQWh8cpT2komyVes0monCDyyQJZoP3IifFuojhs4sV1ZYPKb3isinmp7PbKmajVIBTX0sD5eqZygI2TlD95tt NNTP-Posting-Date: Mon, 25 Apr 2011 19:14:01 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="jRSHUugtWGM8JoY1O2MKRDZMkvNZB1UBzVepI1JNgJdtGj3WMT0Z5fvxrv3EUQRn0noBhPor8ghUxms9kPrm0XdeB6zJZJn4EyZ2Mp8RrUH+GSc/TIrcm1ZRqmOrOEcg"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 In-Reply-To: Cancel-Lock: sha1:Z+deEeWPi3uC9CLdfsbp2dSr0sw= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:653 Fred wrote: > However, it can be dangerous for a constructor to call any method that > a subclass might override, because the superclass constructor would > then invoke the subclass method BEFORE the subclass constructor has > been called, and the subclass method may try to operate on subclass > variables that have not yet been created. > > Examples: Swing's JComponent constructor calls setBackground() and > setFont(). But if your component that is a subclass of JComponent is a > container that creates many other subcomponents, you might want your > setBackground() method to change the backgrounds of itself and all of > its subcomponents. But the JComponent constructor will invoke your > setBackground() before your constructor has created any of those > subcomponents. > > When I write constructors, they NEVER call any method that is not > private. or 'final' -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg