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: markspace <-@.> Newsgroups: comp.lang.java.help Subject: Re: Why can one invoke setters and getters from a (class) constructor? Date: Sat, 23 Apr 2011 11:00:59 -0700 Organization: A noiseless patient Spider Lines: 52 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: Sat, 23 Apr 2011 18:01:07 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="Ar+YdiQzlSTzbQb3yqnkPw"; logging-data="25254"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ltlQzp5y1VGmPaDJjtfNufdym+oEBHQM=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: <87bozwkgz8.fsf@merciadriluca-station.MERCIADRILUCA> Cancel-Lock: sha1:K1E9o5gd6He9RAtstFWGKpHyEu4= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:612 On 4/23/2011 10:36 AM, Merciadri Luca wrote: > But the object is only defined once the > constructor has been parsed totally, isn't it? Not exactly. There still must be some thread of execution that initializes an object instance. That's what you're doing in the constructor. Conceptually, I think of objects (instances) in two parts. The first part is a block of contiguous memory that holds the instance fields and resides in the heap. The second part is the methods of the class. The methods of a class always exist. They're initialized when the class itself is loaded, and there's only one copy of the methods that are shared between all instances (objects) of that class. This part does not live in the heap, it lives in the text segment (text here means "program code," not ASCII; it's called "text" for historical reasons). So you have one class, with the methods, and then 0 or more instances (objects) of that class. Since the methods exist FIRST, when the class itself is loaded, before any objects of that class exist, and since the methods are separate from the objects, it should be easy to see how the methods could be used during initialization. Conceptually, internally, each method has a reference to a block of memory. Once that memory is allocated, each method just refers to that block of memory. Your memory block is allocated before your constructor starts, so voila, there's no problem access your not yet defined objects, because the memory is there already. This method: int getValue() { return value; } becomes something like this int getValue( Object block ) { return block.offset[4]; } So here the field "value" was probably the 2nd field defined and it lives at offset 4 in the memory block your object uses to hold its instance fields. All of this should be very familiar to you from your systems programming courses. I think I did my work in my second or third year of college, in a course on compilers and linkers. Question for you: what year are you at in your Com Sci courses, and have you had compilers yet?