The use of "this" in AdMob Custom Event Class

21 views
Skip to first unread message

Stephen Wong

unread,
Apr 19, 2014, 1:54:43 PM4/19/14
to google-adm...@googlegroups.com

I'm study this article (https://developers.google.com/mobile-ads-sdk/training/mediation/custom-events/banners), and found out "this" is used. Below is a quick summary of the codes:

public class CustomAd implements CustomEventBanner, AdListener {
  private CustomEventBannerListener bannerListener;
  private AdView adView;

  @Override
  public void requestBannerAd(final CustomEventBannerListener listener,
                              final Activity activity,
                              String label,
                              String serverParameter,
                              AdSize adSize,
                              MediationAdRequest mediationAdRequest,
                              Object extra) {


    this.bannerListener = listener;
    this.adView = new AdView(activity, bestAdSize, serverParameter);
    this.adView.setAdListener(this);
    AdRequest adRequest = new AdRequest();
    this.adView.loadAd(adRequest);
  }
}

Here, we can see that the field "adView" is created under the class "CustomAd". In order to reference it, we've used "this.adView". However, I found that even I don't use "this" (so it will be "adView" instead of "this.adView"), everything goes fine without any error.

So what is the purpose of using "this" here?

Thanks.

Eric Leichtenschlag

unread,
Apr 21, 2014, 3:37:35 PM4/21/14
to google-adm...@googlegroups.com
Hi Stephen,

The "this" keyword is optional in this case. People tend to have different thoughts about the use of optional "this", but in this case the code works with or without it.

Thanks,
Eric
Reply all
Reply to author
Forward
0 new messages