Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34138

Re: Another question on Java keyword "this".

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Another question on Java keyword "this".
Date 2016-04-15 08:24 -0400
Organization A noiseless patient Spider
Message-ID <neqmbg$u2t$1@dont-email.me> (permalink)
References <7d6a2a1e-bf53-472c-8ec2-2c92a0b5f73e@googlegroups.com> <bc7d7c11-2f50-4a0e-83dd-25df3d0fde4f@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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