this keyword and this() : difference?

M

Marc Twain

Hi -

This might sound like a silly question but what is the difference
between this() - the keyword used to chain constructors) and this.
(the reference to the current object on which a method is invoked).

I understand what they do but I am curious to know if they are the
same (they both refer to the current object) and how they become
accessible to the programmer.

TIA,

AJ.
 
J

Joona I Palaste

Marc Twain said:
This might sound like a silly question but what is the difference
between this() - the keyword used to chain constructors) and this.
(the reference to the current object on which a method is invoked).

That's the only difference. One is an object reference, the other is a
method call.
I understand what they do but I am curious to know if they are the
same (they both refer to the current object) and how they become
accessible to the programmer.

They are not exactly the same, but they have the same name, and they
both access the same object. What do you mean by "how they become
accessible to the programmer"?
 
B

Brad BARCLAY

Marc said:
This might sound like a silly question but what is the difference
between this() - the keyword used to chain constructors) and this.
(the reference to the current object on which a method is invoked).

This is a reference to the current object, wheras this() is a call to
the default constructor.

They may have the same identifier, but then again so do "int" and
"int[]", and those are two completely different things as well.

HTH!

Brad BARCLAY
 
M

Marc Twain

What do you mean by "how they become
accessible to the programmer"?

Thank you for your reply.

I meant, how come we can use them in our code? For example it is my
understanding that this (the object reference) is passed transparently
to every method call receives 'this' as a method parameter
transparently.

Something like:
(in class Test)
public void doNothing() {
this.aVar = 20;
}

.... is possible onlyh because it's in fact read as:
public void doNothing(Test this) {
this.aVar = 20;
}

Am I right about this? I got the above from a Sun manual.

TIA,

AJ
 
J

Joona I Palaste

Thank you for your reply.
I meant, how come we can use them in our code? For example it is my
understanding that this (the object reference) is passed transparently
to every method call receives 'this' as a method parameter
transparently.
Something like:
(in class Test)
public void doNothing() {
this.aVar = 20;
}
... is possible onlyh because it's in fact read as:
public void doNothing(Test this) {
this.aVar = 20;
}
Am I right about this? I got the above from a Sun manual.

Well, I still don't know exactly what you're after, but it sounds like
you're after an explanation about how exactly "this" and "this()"
become visible to the implementor. The answer is compiler magic. The
JLS says they must, and the compiler/JVM implementor finds a way to
make them. The way above is one way to do it.
So in essence: yes, you're more or less right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top