Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #612
| From | markspace <-@.> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Why can one invoke setters and getters from a (class) constructor? |
| Date | 2011-04-23 11:00 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <iov452$ol6$1@dont-email.me> (permalink) |
| References | <87bozwkgz8.fsf@merciadriluca-station.MERCIADRILUCA> |
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?
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
Why can one invoke setters and getters from a (class) constructor? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-23 19:36 +0200
Re: Why can one invoke setters and getters from a (class) constructor? Lew <noone@lewscanon.com> - 2011-04-23 13:58 -0400
Re: Why can one invoke setters and getters from a (class) constructor? markspace <-@.> - 2011-04-23 11:00 -0700
Re: Why can one invoke setters and getters from a (class) constructor? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-23 23:53 +0200
Re: Why can one invoke setters and getters from a (class) constructor? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-04-23 20:36 -0400
Re: Why can one invoke setters and getters from a (class) constructor? Lew <noone@lewscanon.com> - 2011-04-23 22:37 -0400
Re: Why can one invoke setters and getters from a (class) constructor? Roedy Green <see_website@mindprod.com.invalid> - 2011-04-24 06:53 -0700
Re: Why can one invoke setters and getters from a (class) constructor? Fred <fred.l.kleinschmidt@boeing.com> - 2011-04-25 11:06 -0700
Re: Why can one invoke setters and getters from a (class) constructor? Lew <noone@lewscanon.com> - 2011-04-25 15:14 -0400
csiph-web