For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2007 > what is the 'this' keyword









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author what is the 'this' keyword
K Gaur

2007-02-20, 7:07 pm

hi all,

I was just wondering what the 'this' keyword in java is
is it a pointer like the 'this' pointer in c++ or is it an object
referencing another object?

please elucidate.

thanks

Patricia Shanahan

2007-02-20, 7:07 pm

K Gaur wrote:
> hi all,
>
> I was just wondering what the 'this' keyword in java is
> is it a pointer like the 'this' pointer in c++ or is it an object
> referencing another object?
>
> please elucidate.
>
> thanks
>


The keyword has more than one use. The commonest is as a pointer to the
current object. In general, a Java reference is either null or a pointer
to some object, but *this* can never be null.

*this* is also used when chaining constructors in the same class.

Patricia
M.J. Dance

2007-02-20, 7:07 pm

K Gaur wrote:

> or is it an object referencing another object?


If this were the case, the keyword would be "another" rather than "this",
wouldn't you say?
K Gaur

2007-02-20, 7:07 pm

> The keyword has more than one use. The commonest is as a pointer to the
> current object. In general, a Java reference is either null or a pointer
> to some object, but *this* can never be null.
>
> *this* is also used when chaining constructors in the same class.


those are the application areas of 'this' but what i need to know is
that how is 'this' actually
implemented in java. is it implemented as a pointer?


>If this were the case, the keyword would be "another" rather than "this",
>wouldn't you say?


well I was just trying to think of the possible implementations and I
could not rule that one out
this could be implemented as an object of the Object class

thanks

Patricia Shanahan

2007-02-20, 7:07 pm

K Gaur wrote:
>
> those are the application areas of 'this' but what i need to know is
> that how is 'this' actually
> implemented in java. is it implemented as a pointer?


The *this* keyword means several different things. It does not really
make sense to talk about how it is implemented in Java. It makes more
sense to talk about how the different things it is used for are implemented.

[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.]

Are you asking how instance fields and methods are accessed? Also, how
do you define "pointer"?

Patricia
blmblm@myrealbox.com

2007-02-20, 7:08 pm

In article <erfahq$2tnn$1@ihnp4.ucsd.edu>,
Patricia Shanahan <pats@acm.org> wrote:
> K Gaur wrote:
>
> The *this* keyword means several different things. It does not really
> make sense to talk about how it is implemented in Java. It makes more
> sense to talk about how the different things it is used for are implemented.
>
> [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.]
>
> Are you asking how instance fields and methods are accessed? Also, how
> do you define "pointer"?
>


I wonder if the OP's confusion comes from thinking that in Java,
as in C++, it is possible to have variables that are objects and
also variables that are pointers/references.

To the OP: In Java, variables are primitives (such as ints
and doubles) or references. Period. So for example "String s"
declares not a String object but a reference to a String object.
Does this help answer your question?

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
K Gaur

2007-02-21, 7:16 pm

> The *this* keyword means several different things. It does not really
> make sense to talk about how it is implemented in Java. It makes more
> sense to talk about how the different things it is used for are implemented.


all i would say is that knowing (or trying to know) what goes
behind the scenes never hurt anyone

> [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

K Gaur

2007-02-21, 7:16 pm

thanks

K Gaur

2007-02-21, 7:16 pm

> I wonder if the OP's confusion comes from thinking that in Java,
> as in C++, it is possible to have variables that are objects and
> also variables that are pointers/references.


well i have read that in java there are no pointer variables
available for use by a java programmer. but
pointers are, implicitly used ( how i dont know ).
i only wanted to know if the 'this' is one such implicit
implementation of pointer.

thanks


blmblm@myrealbox.com

2007-02-21, 7:16 pm

In article <1172073540.502990.97500@v33g2000cwv.googlegroups.com>,
K Gaur <gaurkuber@gmail.com> wrote:
>
> well i have read that in java there are no pointer variables
> available for use by a java programmer. but
> pointers are, implicitly used ( how i dont know ).


Well .... What Java has are "references", which in my thinking
are very much like pointers, but without some of the features that
can make pointers messy or dangerous. You can't do arithmetic with
references, as you can with pointers, but a Java linked list with
references is conceptually pretty much the same (maybe I should say
"IMO") as a C++ linked list with pointers.

> i only wanted to know if the 'this' is one such implicit
> implementation of pointer.


I would say it is, but based on some of the other replies (e.g.,
Patricia's reference to hash-table use by the compiler?) I wonder
whether my understanding has some flaws.

Maybe the right question is -- what are you thinking of as the
alternative to something pointer-like? something involving a copy
maybe??

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Jeff

2007-02-21, 7:16 pm

On Feb 20, 10:56 am, "M.J. Dance" <mjda...@hotmail.com> wrote:
> K Gaur wrote:
>
> If this were the case, the keyword would be "another" rather than "this",
> wouldn't you say?



"this" is in essence a pointer to the current object. In Java, objects
are blocks of memory on the memory heap and you use a variable name to
refer to that block - it's called a reference, but is much like a
pointer in behavior. "this" gives you a pointer to the current object
- not a new object formed from the same class or copy of the current
object. So, say the current object is myObject of class TheObject,
with a property called color that can be changed according to a set of
predefined variables:
myObject.color = green; // object is now green
this.color = red; // same object is now red.
TheObject newObject = myObject; // creates a new object identical to
myObject
newObject.color = green; // newObject is now green, myObject stays red
Objects are discarded and marked for garbage collection once the last
reference to the object has been abandoned.


Tor Iver Wilhelmsen

2007-02-21, 7:16 pm

På Wed, 21 Feb 2007 16:59:00 +0100, skrev K Gaur <gaurkuber@gmail.com>:

> well i have read that in java there are no pointer variables
> available for use by a java programmer.


Java does not have C's "memory address" pointers. They are closer to
Pascal's pointer concept. Except you cannot have pointers to primitives.

So: A Java variable either holds a primitive, a "null" pointer or a
pointer to a heap-assigned object.
Lew

2007-02-21, 7:16 pm

Jeff wrote:
> "this" is in essence a pointer to the current object. In Java, objects
> are blocks of memory on the memory heap and you use a variable name to
> refer to that block - it's called a reference, but is much like a
> pointer in behavior. "this" gives you a pointer to the current object
> - not a new object formed from the same class or copy of the current
> object. So, say the current object is myObject of class TheObject,
> with a property called color that can be changed according to a set of
> predefined variables:


This is not precisely correct.

> myObject.color = green; // object is now green
> this.color = red; // same object is now red.


If there is a variable "myFoo" of class "Foo", it is extremely unlikely that
it points to "this" unless there was an assignment
myFoo = this;
which is fairly unusual.

It is more typical for a "myFoo" to be a variable in some other object's
method, and "this" to be used in methods within the object itself. So one
would usually use either "this" or a named variable, but very rarely both for
the same object.

> TheObject newObject = myObject; // creates a new object identical to
> myObject


Wrong - this does not create a new object, but a new reference to the same object.

> newObject.color = green; // newObject is now green, myObject stays red


Wrong - the value of attribute "color" is now green (incidentally, enum values
are conventionally spelled in all UPPERCASE letters) for the single object
pointed to by both variables.

If "color" were an accessible element of the 'TheObject' (yucky name) class,
then 'newObject' and 'myObject' will both point to the exact same object, and
the assignment of the attribute through 'newObject' will be visible through
'myObject'.

- Lew
Chris Smith

2007-02-22, 7:12 pm

K Gaur <gaurkuber@gmail.com> wrote:
>
> 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
Oliver Wong

2007-02-23, 7:15 pm


"K Gaur" <gaurkuber@gmail.com> wrote in message
news:1172073115.435203.74570@k78g2000cwa.googlegroups.com...
>
> all i would say is that knowing (or trying to know) what goes
> behind the scenes never hurt anyone


If you want to know what goes on behind the scenes with respect to the
"this" keyword, when it is used to reference the current object, you may
want to read
http://www-128.ibm.com/developerwor...aggar_bytecode/

Basically (lots of hand waving here), "this" translates to the bytecode
operation "aload_0".

- Oliver


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com