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


Groups > comp.lang.java.programmer > #34109 > unrolled thread

What does keyword 'this' refer to in the following code?

Started bycdalten@gmail.com
First post2016-04-11 21:44 -0700
Last post2016-04-13 05:12 -0700
Articles 13 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  What does keyword 'this' refer to in the following code? cdalten@gmail.com - 2016-04-11 21:44 -0700
    Re: What does keyword 'this' refer to in the following code? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2016-04-12 12:16 +0300
    Re: What does keyword 'this' refer to in the following code? Eric Douglas <e.d.programmer@gmail.com> - 2016-04-12 04:57 -0700
      Re: What does keyword 'this' refer to in the following code? cdalten@gmail.com - 2016-04-12 08:28 -0700
        Re: What does keyword 'this' refer to in the following code? Lew <lewbloch@gmail.com> - 2016-04-12 14:03 -0700
          Re: What does keyword 'this' refer to in the following code? cdalten@gmail.com - 2016-04-12 18:23 -0700
            Re: What does keyword 'this' refer to in the following code? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2016-04-13 11:39 +0300
            Re: What does keyword 'this' refer to in the following code? Eric Douglas <e.d.programmer@gmail.com> - 2016-04-13 05:07 -0700
    Re: What does keyword 'this' refer to in the following code? Jeff Higgins <jeff@invalid.invalid> - 2016-04-12 22:00 +0000
      Re: What does keyword 'this' refer to in the following code? cdalten@gmail.com - 2016-04-12 20:03 -0700
        Re: What does keyword 'this' refer to in the following code? cdalten@gmail.com - 2016-04-12 21:01 -0700
        Re: What does keyword 'this' refer to in the following code? Lars Enderin <lars.enderin@telia.com> - 2016-04-13 10:58 +0200
        Re: What does keyword 'this' refer to in the following code? Eric Douglas <e.d.programmer@gmail.com> - 2016-04-13 05:12 -0700

#34109 — What does keyword 'this' refer to in the following code?

Fromcdalten@gmail.com
Date2016-04-11 21:44 -0700
SubjectWhat does keyword 'this' refer to in the following code?
Message-ID<2a655d5f-3df0-4bf2-a2bc-4b70406d7ab2@googlegroups.com>
https://developers.google.com/maps/documentation/android-api/

Does 'this' refer to refer to the instance of MapPane or Activity?

[toc] | [next] | [standalone]


#34110

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2016-04-12 12:16 +0300
Message-ID<lvzisz2nm4.fsf@saunalahti.fi>
In reply to#34109
cdalten@gmail.com writes:

> https://developers.google.com/maps/documentation/android-api/
> Does 'this' refer to refer to the instance of MapPane or Activity?

The MapPane instance where the call happens, but it is also an Acitivty,
because MapPane extends Activity.

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#34112

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-04-12 04:57 -0700
Message-ID<39484e69-456f-449d-9c2d-0978ea711b1e@googlegroups.com>
In reply to#34109
On Tuesday, April 12, 2016 at 12:44:31 AM UTC-4, cda...@gmail.com wrote:
> https://developers.google.com/maps/documentation/android-api/
> 
> Does 'this' refer to refer to the instance of MapPane or Activity?

yes
MapPane extends Activity, so it refers to an instance of MapPane and can be used to refer to a MapPane as an Activity.
What are you getting at?

[toc] | [prev] | [next] | [standalone]


#34113

Fromcdalten@gmail.com
Date2016-04-12 08:28 -0700
Message-ID<67585c09-1a90-49c6-a78a-a20628965620@googlegroups.com>
In reply to#34112
I think the keyword super is what threw me off.

[toc] | [prev] | [next] | [standalone]


#34114

FromLew <lewbloch@gmail.com>
Date2016-04-12 14:03 -0700
Message-ID<c39d6983-ce6a-48f5-bd9b-862d0c54b468@googlegroups.com>
In reply to#34113
On Tuesday, April 12, 2016 at 8:28:33 AM UTC-7, cda...@gmail.com wrote:
> I think the keyword super is what threw me off.

It's a bog-standard use of 'super':
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 ...

What is your confusion?

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#34116

Fromcdalten@gmail.com
Date2016-04-12 18:23 -0700
Message-ID<f7f0207d-dddb-45e3-bf49-c993416e41cd@googlegroups.com>
In reply to#34114
On Tuesday, April 12, 2016 at 2:04:16 PM UTC-7, Lew wrote:
> On Tuesday, April 12, 2016 at 8:28:33 AM UTC-7, cda...@gmail.com wrote:
> > I think the keyword super is what threw me off.
> 
> It's a bog-standard use of 'super':
>    @Override
>     protected void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>  ...
> 
> What is your confusion?
> 

For whatever reasons, I thought the keyword super impacted what 'this' refers to.

[toc] | [prev] | [next] | [standalone]


#34119

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2016-04-13 11:39 +0300
Message-ID<lv7fg1sy04.fsf@saunalahti.fi>
In reply to#34116
cdalten@gmail.com writes:
> On Tuesday, April 12, 2016 at 2:04:16 PM UTC-7, Lew wrote:
>> On Tuesday, April 12, 2016 at 8:28:33 AM UTC-7, cda...@gmail.com wrote:
>> > I think the keyword super is what threw me off.

>>     protected void onCreate(Bundle savedInstanceState) {
>>         super.onCreate(savedInstanceState);

> For whatever reasons, I thought the keyword super impacted what 'this'
> refers to.

It just makes the onCreate method first execute the onCreate method of
the superclass before doing the rest of its business.

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#34121

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-04-13 05:07 -0700
Message-ID<e3e29a3f-d7e4-4ef7-8690-9980255f52ea@googlegroups.com>
In reply to#34116
On Tuesday, April 12, 2016 at 9:23:53 PM UTC-4, cda...@gmail.com wrote:
> For whatever reasons, I thought the keyword super impacted what 'this' refers to.

Nope, this refers to an instance of current class.  super calls the method of the class extended by the current class.  These keywords are not related.

[toc] | [prev] | [next] | [standalone]


#34115

FromJeff Higgins <jeff@invalid.invalid>
Date2016-04-12 22:00 +0000
Message-ID<nejr5l$8c9$1@dont-email.me>
In reply to#34109
On Mon, 11 Apr 2016 21:44:21 -0700, cdalten wrote:

> https://developers.google.com/maps/documentation/android-api/
> 
> Does 'this' refer to refer to the instance of MapPane or Activity?

It refers to the instance of MapPane as an
com.google.android.gms.maps.OnMapReadyCallback


import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;

public class MapPane extends Activity implements OnMapReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {

  /*
   * From the android.app.Activity javadoc:
   * Derived classes must call through to the super class's 
   * implementation of this method. If they do not,
   * an exception will be thrown.
   */
  super.onCreate(savedInstanceState);

  setContentView(R.layout.map_activity);

  MapFragment mapFragment = (MapFragment) getFragmentManager()
    .findFragmentById(R.id.map);

  /*
   * MapFragment.getMapAsync (OnMapReadyCallback callback)
   * 
   * public interface OnMapReadyCallback {
   *   abstract void onMapReady(GoogleMap googleMap)
   * }
   *
   * So here, "this" refers to this instance of MapPane
   * as an com.google.android.gms.maps.OnMapReadyCallback
   */
  mapFragment.getMapAsync(this);
}


  /*
   * Implementing com.google.android.gms.maps.OnMapReadyCallback
   */
@Override
public void onMapReady(GoogleMap map) {
  LatLng sydney = new LatLng(-33.867, 151.206);

  map.setMyLocationEnabled(true);
  map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

  map.addMarker(new MarkerOptions()
    .title("Sydney")
    .snippet("The most populous city in Australia.")
  .position(sydney));
}
}

[toc] | [prev] | [next] | [standalone]


#34117

Fromcdalten@gmail.com
Date2016-04-12 20:03 -0700
Message-ID<0ecdea31-577e-4117-b6f0-c0a9d42bf3f3@googlegroups.com>
In reply to#34115
On Tuesday, April 12, 2016 at 3:03:56 PM UTC-7, Jeff Higgins wrote:
> On Mon, 11 Apr 2016 21:44:21 -0700, cdalten wrote:
> 
> > https://developers.google.com/maps/documentation/android-api/
> > 
> > Does 'this' refer to refer to the instance of MapPane or Activity?
> 
> It refers to the instance of MapPane as an
> com.google.android.gms.maps.OnMapReadyCallback
> 
> 
> import com.google.android.gms.maps.*;
> import com.google.android.gms.maps.model.*;
> import android.app.Activity;
> import android.os.Bundle;
> 
> public class MapPane extends Activity implements OnMapReadyCallback {
> 
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> 
>   /*
>    * From the android.app.Activity javadoc:
>    * Derived classes must call through to the super class's 
>    * implementation of this method. If they do not,
>    * an exception will be thrown.
>    */
>   super.onCreate(savedInstanceState);
> 
>   setContentView(R.layout.map_activity);
> 
>   MapFragment mapFragment = (MapFragment) getFragmentManager()
>     .findFragmentById(R.id.map);
> 
>   /*
>    * MapFragment.getMapAsync (OnMapReadyCallback callback)
>    * 
>    * public interface OnMapReadyCallback {
>    *   abstract void onMapReady(GoogleMap googleMap)
>    * }
>    *
>    * So here, "this" refers to this instance of MapPane
>    * as an com.google.android.gms.maps.OnMapReadyCallback
>    */
>   mapFragment.getMapAsync(this);
> }
> 
> 
>   /*
>    * Implementing com.google.android.gms.maps.OnMapReadyCallback
>    */
> @Override
> public void onMapReady(GoogleMap map) {
>   LatLng sydney = new LatLng(-33.867, 151.206);
> 
>   map.setMyLocationEnabled(true);
>   map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
> 
>   map.addMarker(new MarkerOptions()
>     .title("Sydney")
>     .snippet("The most populous city in Australia.")
>   .position(sydney));
> }
> }

I have another question. What's the significance of map.addMarker().title().snippet().position()?

Why not just do something like
map.addMarker()
map.title()
map.snippet()
map.position()
?

[toc] | [prev] | [next] | [standalone]


#34118

Fromcdalten@gmail.com
Date2016-04-12 21:01 -0700
Message-ID<01ce86dd-a8c0-4a80-80e1-fce2f355b318@googlegroups.com>
In reply to#34117
Let's try this again...

What does the dot to the dot to the dot mean in the following lines of code...

map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
   

[toc] | [prev] | [next] | [standalone]


#34120

FromLars Enderin <lars.enderin@telia.com>
Date2016-04-13 10:58 +0200
Message-ID<570E0A1E.6070608@telia.com>
In reply to#34117
2016-04-13 05:03 skrev  cdalten@gmail.com:
> I have another question. What's the significance of map.addMarker().title().snippet().position()?
> 
> Why not just do something like
> map.addMarker()
> map.title()
> map.snippet()
> map.position()
> ?

It's a shorthand, assuming that each method in the chain returns *this*.
If you look at the source for java.lang.StringBuilder, for example,
you'll find methods like

    public StringBuilder append(int i) {
        IntegralToString.appendInt(this, i);
        return this;
    }
and
    public StringBuilder append(double d) {
        RealToString.getInstance().appendDouble(this, d);
        return this;
    }

so you can write statements like
StringBuilder sb = new StringBuilder().append("Pi = ").append(Math.PI);

-- 
Lars Enderin

[toc] | [prev] | [next] | [standalone]


#34122

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-04-13 05:12 -0700
Message-ID<b0707fa4-8541-44f1-8af0-fab09e248ce6@googlegroups.com>
In reply to#34117
On Tuesday, April 12, 2016 at 11:03:47 PM UTC-4, cda...@gmail.com wrote:
> I have another question. What's the significance of map.addMarker().title().snippet().position()?
> 
This code calls methods on the return value, which may be passing back the object you passed in or a new object.  For instance the String class is immutable so it's methods return new objects, ie
myString.substring(0,2).trim()

> Why not just do something like
> map.addMarker()
> map.title()
> map.snippet()
> map.position()
> ?

This just calls multiple methods on a single object instance.  This is the same as the first format, only if each method is returning the object you passed into it.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web