Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34190
| From | Brixomatic <brixomatic@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Another question on Java keyword "this". |
| Date | 2016-04-23 15:01 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <MPG.31859067f6396cc29896b1@news.aioe.org> (permalink) |
| References | <7d6a2a1e-bf53-472c-8ec2-2c92a0b5f73e@googlegroups.com> <bc7d7c11-2f50-4a0e-83dd-25df3d0fde4f@googlegroups.com> <e7781c42-b917-47fa-bdfb-9f4574208cb8@googlegroups.com> |
In article <e7781c42-b917-47fa-bdfb-9f4574208cb8@googlegroups.com>,
cdalten@gmail.com (cdalten@gmail.com) says...
>
> I swear, trying to learn keyword "this" is just as bad as trying to learn pointers in C.
Actually, it's pretty easy.
It always refers to the object that encloses the statememt.
Try the code below. Note that the same hashCode refers to the same
object.
You will see that every "new" creates a new object, and the "this"
inside that object refers to this object instance.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
class MyClass {
public MyClass() {
System.out.println("new MyClass, this = " + this);
}
void doSomething() {
System.out
.println("MyClass#doSomething(), this = " + this);
final JButton button = new JButton() {
{
System.out.println("new Button, this = " + this);
}
@Override
public String toString() {
return "Button " + hashCode();
}
};
button.addActionListener(new ActionListener() {
{
System.out
.println("new ActionListener, this = " + this);
System.out.println(
"new ActionListener, MyClass.this = " + MyClass.this);
}
@Override
public void actionPerformed(final ActionEvent e) {
System.out.println("actionPerformed, this = " + this);
System.out.println(
"actionPerformed, MyClass.this = " + MyClass.this);
}
@Override
public String toString() {
return "ActionListener " + hashCode();
}
});
System.out.println("doClick()");
button.doClick();
System.out.println("doClick()");
button.doClick();
}
@Override
public String toString() {
return "MyClass " + hashCode();
}
public static void main(final String[] args) {
final MyClass a = new MyClass();
a.doSomething();
a.doSomething();
System.out.println("--- ");
final MyClass b = new MyClass();
b.doSomething();
b.doSomething();
}
}
here is a sample output:
new MyClass, this = MyClass 31054905
MyClass#doSomething(), this = MyClass 31054905
new Button, this = Button 22293109
new ActionListener, this = ActionListener 29094346
new ActionListener, MyClass.this = MyClass 31054905 //same as above
doClick()
actionPerformed, this = ActionListener 29094346 //same as created above
actionPerformed, MyClass.this = MyClass 31054905 //same as created above
doClick()
actionPerformed, this = ActionListener 29094346 //reuses the object
actionPerformed, MyClass.this = MyClass 31054905 //reuses the object
MyClass#doSomething(), this = MyClass 31054905 //same as above
new Button, this = Button 33492446 //new button is created
new ActionListener, this = ActionListener 31952838 //with new Listener
new ActionListener, MyClass.this = MyClass 31054905
doClick()
actionPerformed, this = ActionListener 31952838
actionPerformed, MyClass.this = MyClass 31054905
doClick()
actionPerformed, this = ActionListener 31952838
actionPerformed, MyClass.this = MyClass 31054905
---
new MyClass, this = MyClass 26219575 //new MyClass instance created
MyClass#doSomething(), this = MyClass 26219575
new Button, this = Button 2056742
new ActionListener, this = ActionListener 6613606
new ActionListener, MyClass.this = MyClass 26219575
doClick()
actionPerformed, this = ActionListener 6613606
actionPerformed, MyClass.this = MyClass 26219575
doClick()
actionPerformed, this = ActionListener 6613606
actionPerformed, MyClass.this = MyClass 26219575
MyClass#doSomething(), this = MyClass 26219575
new Button, this = Button 22608339
new ActionListener, this = ActionListener 7971559
new ActionListener, MyClass.this = MyClass 26219575
doClick()
actionPerformed, this = ActionListener 7971559
actionPerformed, MyClass.this = MyClass 26219575
doClick()
actionPerformed, this = ActionListener 7971559
actionPerformed, MyClass.this = MyClass 26219575
Kind regards,
Wanja
--
..Alesi's problem was that the back of the car was jumping up and down
dangerously - and I can assure you from having been teammate to
Jean Alesi and knowing what kind of cars that he can pull up with,
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Another question on Java keyword "this". cdalten@gmail.com - 2016-04-14 23:27 -0700
Re: Another question on Java keyword "this". Eric Douglas <e.d.programmer@gmail.com> - 2016-04-15 04:59 -0700
Re: Another question on Java keyword "this". cdalten@gmail.com - 2016-04-15 05:14 -0700
Re: Another question on Java keyword "this". cdalten@gmail.com - 2016-04-15 05:31 -0700
Re: Another question on Java keyword "this". Nigel Wade <nmw@ion.le.ac.uk> - 2016-04-15 14:05 +0100
Re: Another question on Java keyword "this". Eric Douglas <e.d.programmer@gmail.com> - 2016-04-15 06:25 -0700
Re: Another question on Java keyword "this". Lew <lewbloch@gmail.com> - 2016-04-15 14:48 -0700
Re: Another question on Java keyword "this". Brixomatic <brixomatic@yahoo.com> - 2016-04-23 15:01 +0200
Re: Another question on Java keyword "this". Roedy Green <inquiry@mindprod.com> - 2016-06-04 03:15 -0700
Re: Another question on Java keyword "this". Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2016-06-04 14:34 +0300
Re: Another question on Java keyword "this". Mike Amling <mamling@chaff.us> - 2016-06-04 14:07 -0500
Re: Another question on Java keyword "this". Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-04-15 08:24 -0400
Re: Another question on Java keyword "this". cdalten@gmail.com - 2016-04-15 14:45 -0700
Re: Another question on Java keyword "this". Lew <lewbloch@gmail.com> - 2016-04-15 14:52 -0700
Re: Another question on Java keyword "this". Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-04-15 18:26 -0400
Re: Another question on Java keyword "this". cdalten@gmail.com - 2016-04-17 19:13 -0700
Re: Another question on Java keyword "this". Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-04-18 07:37 -0400
csiph-web