Re: what is the 'this' keyword
- From: Chris Smith <cdsmith@xxxxxxx>
- Date: Thu, 22 Feb 2007 13:58:03 -0700
K Gaur <gaurkuber@xxxxxxxxx> wrote:
[The literal, but unhelpful, answer to how "this" is actually
implemented in Java is that I would expect the compiler to have some
sort of hash table indexed by the keywords.]
well if it is that way that did help
I think you may have misinterpreted what was intended to be a humorous
remark. What Patricia means is that when a compiler is reading the
source code, it probably uses a hash table of some kind to recognize
that the four characters 't', 'h', 'i', and 's' form the keyword "this".
This is unlikely to have answered your real question, which is
presumably why Patricia said it is an unhelpful answer.
Perhaps if I give you one possible answer, that will help. You should
realize that I'm not giving you "the" answer, because implementation
techniques can vary between compilers and Java virtual machines. That
said, one way that the compiler can give you a "this" pointer from
inside an instance method is that when it calls the method, it passes
along a pointer to the object as a sort of hidden parameter.
Specifically, a likely way to implement instance method calls in Java
would look something like this:
1. Follow the object pointer, and look at a predetermined offset to
find a pointer to a class descriptor.
2. Look in some predetermined offset in the class descriptor
to find a pointer to the code for the desired method.
3. Push the object pointer to the stack.
4. Push the parameters to the stack.
5. Call the method's code.
So the object pointer is on the stack from step 3, at a known offset
from the stack pointer (because the method implementation knows how
large its arguments are). When you write "this", the (probably JIT)
compiler generates code to look at that location and get the object
pointer there.
There are lot of different ways this could be written, but that's a
reasonable one. (Also, there are other less common uses for the this
pointer; if you're asking about uses of this to chain constructors, or
something else, then let us know.)
--
Chris Smith
.
- References:
- what is the 'this' keyword
- From: K Gaur
- Re: what is the 'this' keyword
- From: Patricia Shanahan
- Re: what is the 'this' keyword
- From: K Gaur
- Re: what is the 'this' keyword
- From: K Gaur
- what is the 'this' keyword
- Prev by Date: Re: ensuring unique object
- Next by Date: Re: ensuring unique object
- Previous by thread: Re: what is the 'this' keyword
- Next by thread: Re: what is the 'this' keyword
- Index(es):
Relevant Pages
|