Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #619

Re: Why can one invoke setters and getters from a (class) constructor?

Newsgroups comp.lang.java.help
Subject Re: Why can one invoke setters and getters from a (class) constructor?
References <87bozwkgz8.fsf@merciadriluca-station.MERCIADRILUCA> <iov452$ol6$1@dont-email.me>
From Merciadri Luca <Luca.Merciadri@student.ulg.ac.be>
Organization ULg
Date 2011-04-23 23:53 +0200
Message-ID <8762q4my7o.fsf@merciadriluca-station.MERCIADRILUCA> (permalink)

Show all headers | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

markspace <-@.> writes:

> 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?
Somewhere between 2nd and 3rd year (we have 5 years here), but the
first year was completely devoted to things which have absolutely no
link with computer science.

And, no, I haven't had compilers yet. Sure it would help in many
situations.

Thanks for the answers.
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

Give a man a fish and you feed him for a day; teach a man to fish
  and you feed him for a lifetime.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2zSksACgkQM0LLzLt8Mhz/gACglfrE5wx3JDaCc4b8OYX6WWRi
GVwAn2gjkirfOgsv3NRSzJDr5hhNjACu
=H6CD
-----END PGP SIGNATURE-----

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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