Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Joshua Cranmer Newsgroups: comp.lang.java.help Subject: Re: Why can one invoke setters and getters from a (class) constructor? Date: Sat, 23 Apr 2011 20:36:51 -0400 Organization: A noiseless patient Spider Lines: 40 Message-ID: References: <87bozwkgz8.fsf@merciadriluca-station.MERCIADRILUCA> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 24 Apr 2011 00:36:52 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="LtjcJP1H6uHOtkcPMh0bUA"; logging-data="14429"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QPJaqgCLnsaOJQfjuQkaeTKnCaagCYoo=" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.16pre) Gecko/20110305 Lightning/1.0b3pre Thunderbird/3.1.10pre In-Reply-To: <87bozwkgz8.fsf@merciadriluca-station.MERCIADRILUCA> Cancel-Lock: sha1:6r0DDvWYtPiOjqip1s4+K7O+/1E= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:631 On 04/23/2011 01:36 PM, Merciadri Luca wrote: > My question might appear stupid, and unfounded, but I'm wondering why > I can invoke (i.e. use) setters and getters in a (class) > constructor. Setters and getters are defined over the object whose > type is defined by the class. But the object is only defined once the > constructor has been parsed totally, isn't it? Shorter answer: the object is defined before the constructor is called. Short answer: The object is defined--but possibly not fully initialized--at the time the constructor is called. Long answer: By the time constructor has been called, it has been guaranteed that the object's memory has been initialized to the default values (i.e., 0 or the equivalent of 0 for the appropriate object type). Therefore, it makes sense to refer to "this" within the constructor, so it is possible to call anything that refers to "this" (including instance methods). However, the subclasses' constructor will not have been called by this point in time, so calling a method that could be overridden by a subclass might prove problematic. Longer answer: So, the initialization of memory happens pretty much at the point you type in "new" (or the equivalent newInstance statement when doing reflection). This causes all of the local variables to be default-initialized to 0. Immediately thereafter (unless someone is playing with bytecode), the appropriate constructor is called, which then immediately (again, unless someone is playing with bytecode) calls the superclass's constructor, etc., until it reaches Object. At that point, the calls start to unwind. Note that subclasses do not get a chance to really run until after their superclass does, so although it is possible to call overridable instance methods, it is not recommended to do so because said instance method might expect more initialization than is the case. If you do do this, make sure that you emphasize this in documentation so implementors at least know that they need to be prepared for this case. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth