Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34133 > unrolled thread
| Started by | cdalten@gmail.com |
|---|---|
| First post | 2016-04-14 23:27 -0700 |
| Last post | 2016-04-18 07:37 -0400 |
| Articles | 17 — 9 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | cdalten@gmail.com |
|---|---|
| Date | 2016-04-14 23:27 -0700 |
| Subject | Another question on Java keyword "this". |
| Message-ID | <7d6a2a1e-bf53-472c-8ec2-2c92a0b5f73e@googlegroups.com> |
In the follow code found at... https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/GroundOverlayDemoActivity.java What does "this" in map.setOnGroundOverlayClickListener(this); refer to?
[toc] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2016-04-15 04:59 -0700 |
| Message-ID | <bc7d7c11-2f50-4a0e-83dd-25df3d0fde4f@googlegroups.com> |
| In reply to | #34133 |
On Friday, April 15, 2016 at 2:27:34 AM UTC-4, cda...@gmail.com wrote:
> In the follow code found at...
>
> https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/GroundOverlayDemoActivity.java
>
> What does "this" in
>
> map.setOnGroundOverlayClickListener(this);
>
> refer to?
Same as any, it refers to the class, in this case an instance of GroundOverlayDemoActivity.
The only time the keyword this should be confusing is when you have an inner/anonymous class.
For example:
public class MyButton extends JButton {
public MyButton() {
addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
Object o = this;
}
});
}
}
In this case keyword this refers to an instance of an ActionListener, not a MyButton.
[toc] | [prev] | [next] | [standalone]
| From | cdalten@gmail.com |
|---|---|
| Date | 2016-04-15 05:14 -0700 |
| Message-ID | <e7781c42-b917-47fa-bdfb-9f4574208cb8@googlegroups.com> |
| In reply to | #34136 |
I swear, trying to learn keyword "this" is just as bad as trying to learn pointers in C.
[toc] | [prev] | [next] | [standalone]
| From | cdalten@gmail.com |
|---|---|
| Date | 2016-04-15 05:31 -0700 |
| Message-ID | <da4c5a65-5f66-4e59-926a-f26d3da2faf0@googlegroups.com> |
| In reply to | #34137 |
So would keyword "this" in mTransparencyBar.setOnSeekBarChangeListener(this); refer to an instance of OnSeekBarChangeListener? Just curious because mTransparencyBar.setOnSeekBarChangeListener(this); is in the same method.
[toc] | [prev] | [next] | [standalone]
| From | Nigel Wade <nmw@ion.le.ac.uk> |
|---|---|
| Date | 2016-04-15 14:05 +0100 |
| Message-ID | <dnc78dF3surU1@mid.individual.net> |
| In reply to | #34139 |
On 15/04/16 13:31, cdalten@gmail.com wrote: > So would keyword "this" in > > mTransparencyBar.setOnSeekBarChangeListener(this); > > refer to an instance of OnSeekBarChangeListener? > > Just curious because > > mTransparencyBar.setOnSeekBarChangeListener(this); > > is in the same method. > > It refers to the object on which the method containing that statement was invoked. That method is a member of the class GroundOverlayDemoActivity, so it must refer to an instance of that class. It does not refer to an instance of OnSeekBarChangeListener. OnSeekBarChangeListener is an interface and cannot be instantiated. It must refer to an instance of a class which has implemented the interface OnSeekBarChangeListener.
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2016-04-15 06:25 -0700 |
| Message-ID | <4f99552f-3905-4706-b916-b5b3a278c2b2@googlegroups.com> |
| In reply to | #34140 |
On Friday, April 15, 2016 at 9:05:29 AM UTC-4, Nigel Wade wrote: > It does not refer to an instance of OnSeekBarChangeListener. > OnSeekBarChangeListener is an interface and cannot be instantiated. > It must refer to an instance of a class which has implemented the interface OnSeekBarChangeListener. Technically you can't instantiate an interface, so it refers to an instance of the class that implements the interface, which is where it gets fuzzy as in my ActionListener example. Sometimes I want that method passing in the new ActionListener() to refer to the button, then I have to assign the button before the statement that sets up that ActionListener, like final MyButton thisButton = this;
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2016-04-15 14:48 -0700 |
| Message-ID | <2151e42c-874e-4b10-840b-5c1141be9343@googlegroups.com> |
| In reply to | #34139 |
On Friday, April 15, 2016 at 5:31:48 AM UTC-7, cda...@gmail.com wrote: > So would keyword "this" in > mTransparencyBar.setOnSeekBarChangeListener(this); > refer to an instance of OnSeekBarChangeListener? No. It refers to the innermost containing class instance. > Just curious because > mTransparencyBar.setOnSeekBarChangeListener(this); > is in the same method. Why not read the Tutorial? https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Brixomatic <brixomatic@yahoo.com> |
|---|---|
| Date | 2016-04-23 15:01 +0200 |
| Message-ID | <MPG.31859067f6396cc29896b1@news.aioe.org> |
| In reply to | #34137 |
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]
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <inquiry@mindprod.com> |
|---|---|
| Date | 2016-06-04 03:15 -0700 |
| Message-ID | <aba5lbti1vrodvksmkifkfponinusja9uf@4ax.com> |
| In reply to | #34137 |
On Fri, 15 Apr 2016 05:14:07 -0700 (PDT), cdalten@gmail.com wrote: >I swear, trying to learn keyword "this" is just as bad as trying to learn pointers in C. you can always add the class name to this to make it clean. eg. MyOuters.this so that you can refer to an outer class object from an inner class one. I have written a newbie tutorial on "this" you might find useful http://mindprod.com/jgloss/this.html If parts of it are confusing, let me know and perhaps I can make them comprehensible.
[toc] | [prev] | [next] | [standalone]
| From | Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> |
|---|---|
| Date | 2016-06-04 14:34 +0300 |
| Message-ID | <lv4m9989x9.fsf@saunalahti.fi> |
| In reply to | #34458 |
Roedy Green <inquiry@mindprod.com> writes: > you can always add the class name to this to make it clean. eg. > MyOuters.this so that you can refer to an outer class object from an > inner class one. Needing to do that may suggest a design flaw somewhere. The super keyword may be used to call super class methods. And if you want to use attributes from a super class, you shouldn't shadow them in the extending class. -- Jukka Lahtinen
[toc] | [prev] | [next] | [standalone]
| From | Mike Amling <mamling@chaff.us> |
|---|---|
| Date | 2016-06-04 14:07 -0500 |
| Message-ID | <niv8rg$ml9$1@dont-email.me> |
| In reply to | #34459 |
On 6/4/16 6:34 AM, Jukka Lahtinen wrote: > Roedy Green <inquiry@mindprod.com> writes: > >> you can always add the class name to this to make it clean. eg. >> MyOuters.this so that you can refer to an outer class object from an >> inner class one. > > Needing to do that may suggest a design flaw somewhere. > The super keyword may be used to call super class methods. > And if you want to use attributes from a super class, you shouldn't > shadow them in the extending class. MyOuter.this and super are two different things. super refers to the one and only class that this class extends. MyOuter.this refers to an instance of any of the classes that this class is nested in. --Mike Amling RXhhbXBsZXMgb24gcmVxdWVzdC4=
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-04-15 08:24 -0400 |
| Message-ID | <neqmbg$u2t$1@dont-email.me> |
| In reply to | #34136 |
On 4/15/2016 7:59 AM, Eric Douglas wrote:
> On Friday, April 15, 2016 at 2:27:34 AM UTC-4, cda...@gmail.com wrote:
>> In the follow code found at...
>>
>> https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/GroundOverlayDemoActivity.java
>>
>> What does "this" in
>>
>> map.setOnGroundOverlayClickListener(this);
>>
>> refer to?
>
> Same as any, it refers to the class, in this case an instance of GroundOverlayDemoActivity.
Just to clarify: `this' does not refer to a class, but to the
"current instance" of a class: The object on which the method has
been invoked (or, in a constructor, the object being constructed.)
To refer to the class, you'd write `this.getClass()'.
There's also another -- and rather different -- use of `this':
In a class with several constructors, one constructor can "delegate"
to another by using `this' to invoke it:
public class Thing {
private static final int DEFAULT_DOODAD = 42;
private final int doodad;
public Thing(int doodad) {
this.doodad = doodad; // `this' in the first sense
}
public Thing() {
this(DEFAULT_DOODAD); // `this' in the second sense
}
}
The first `this' refers to the object being constructed, and the line
says "Set the `doodad' field of that object to the value of the
`doodad' parameter." The second `this' does not refer to any object
at all, but invokes the first constructor.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | cdalten@gmail.com |
|---|---|
| Date | 2016-04-15 14:45 -0700 |
| Message-ID | <8bd83f19-b47f-43e8-9acb-d3d4ed125a2a@googlegroups.com> |
| In reply to | #34133 |
On Thursday, April 14, 2016 at 11:27:34 PM UTC-7, cda...@gmail.com wrote: > In the follow code found at... > > https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/GroundOverlayDemoActivity.java > > What does "this" in > > map.setOnGroundOverlayClickListener(this); > > refer to? I thought about it. I think I found out what's irking me. Does map.setOnGroundOverlayClickListener(this); Implement something? If so, what does it implement?
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2016-04-15 14:52 -0700 |
| Message-ID | <81ea3fca-b3ea-40a8-bd4f-37df44abde4c@googlegroups.com> |
| In reply to | #34144 |
On Friday, April 15, 2016 at 2:45:33 PM UTC-7, cda...@gmail.com wrote: > I thought about it. I think I found out what's irking me. Does > map.setOnGroundOverlayClickListener(this); > Implement something? If so, what does it implement? The keyword "implements" in Java means that a class declares an override of an abstract method. Since the method you ask about is on the 'map' instance, not on the containing class, we'd have to know the type of the 'map' variable to know if 'setOnGroundOverlayClickListener()' is inherited by that type. In a general sense, every concrete (that is, non-abstract) method implements something, namely the logic of the method. In a Java-specific sense, the method only "implements" something if it overrides an abstract method that it inherited. Why not read the Java Tutorials? https://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-04-15 18:26 -0400 |
| Message-ID | <nerpka$c7n$1@dont-email.me> |
| In reply to | #34144 |
On 4/15/2016 5:45 PM, cdalten@gmail.com wrote:
> On Thursday, April 14, 2016 at 11:27:34 PM UTC-7, cda...@gmail.com wrote:
>> In the follow code found at...
>>
>> https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/GroundOverlayDemoActivity.java
>>
>> What does "this" in
>>
>> map.setOnGroundOverlayClickListener(this);
>>
>> refer to?
>
> I thought about it. I think I found out what's irking me. Does
>
> map.setOnGroundOverlayClickListener(this);
>
> Implement something? If so, what does it implement?
The identifier `map' refers to some kind of object. (It may refer
to it by the object's actual class name, or by the name of a superclass,
or by the name of an interface implemented by the object's class -- but
in any event, `map' refers to some kind of an object.)
The referred-to object is an instance of a class that has (or maybe
inherits) a method named `setOnGroundOverlayClickListener'. That
method has one parameter (or possibly more, but that would just
confuse you to the point where your head might explode), and that
parameter is declared with some particular type: Again, a class or
perhaps an interface.
Assuming that the code compiles, the object referred to by `this'
belongs to a class that is compatible with the method's parameter.
By "compatible with," I mean that the `this' object is either of the
class declared for the parameter, or is a subclass of that class, or
(if the parameter is declared with an interface type) is a class that
implements that interface or one of its sub-interfaces.
... and all of this is *elementary*, the kind of stuff that's
covered in "Java 101," probably by the fifth or sixth class meeting
and certainly before the tenth. I really think you should heed Lew's
advice about doing some introductory reading before diving in with
questions at the level of "What's an `int'?" Usenet is a fine medium
for handling directed inquiries, but a poor one for the mass transfer
of basic information. It's like taking a correspondence course over
Twitter. Seek other approaches.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | cdalten@gmail.com |
|---|---|
| Date | 2016-04-17 19:13 -0700 |
| Message-ID | <61515233-92a5-4303-802a-fc3f03ec00ba@googlegroups.com> |
| In reply to | #34148 |
Reading the tutorials would be too easy.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-04-18 07:37 -0400 |
| Message-ID | <nf2gn3$kiq$1@dont-email.me> |
| In reply to | #34160 |
On 4/17/2016 10:13 PM, cdalten@gmail.com wrote:
> Reading the tutorials would be too easy.
Killfiles are easy, too.
Seriously: People will weary of trying to help you if they
perceive that you're expending little effort of your own. Lots
of people are happy to help; few are happy to spoon-feed. Invest
a bit of your own time, ask questions about the hard parts --
that's fine, that's what Usenet is good at.
Besides: If you know a bit about the subject matter, you'll
be able to frame better questions, get better answers, and learn
more effectively.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web