3

I'm study an article, 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.

Kit Ng
  • 993
  • 4
  • 12
  • 24
  • 3
    **Please read [this](http://stackoverflow.com/questions/2429062/java-when-to-use-this-keyword)**. – tilpner Apr 19 '14 at 17:57
  • **See this link it will help you.** http://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java[1] –  Apr 19 '14 at 18:01

2 Answers2

3

In the case above the this keyword is not required.

However if, for example the method parameter was also called bannerListener, using the this keyword on the left-hand side would specify that you are referring to the member field and not the parameter.

In the example above it's probably used to aid readability. It's not necessary to reference a member field that way, but it does make it explicit and can prevent accidentally referencing the wrong thing.

From the docs:

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • 1
    Wow, it makes me so clear. I'll study it for a while and mark it as answer if everything is ok. Thanks! – Kit Ng Apr 21 '14 at 20:14
2

In the example you provided, this is only necessary on the adView variable. The this keyword refers to this current object. If you look at your method header, you have a variable passed in called adView and you have a field called adView. If I just say adView.loadAd(/** something */), which adView am I talking about? Do I mean the parameter or the field? As far as java is concerned, I am asking for the parameter, not the field, but how does the programmer know that without that prior knowledge? If you changed the parameter variable to something else, then you could avoid using this, but I would advise against it. Naming parameters to match your fields makes it very clear what you intend to do with it, and using the this keyword on fields makes it very clear which one you are referring to.

Edit: Missed a part of the question. You're right in that it removing this will work fine for that method, but your adView field is never assigned to anything, which makes it null. What happens if later in your code, in a different method, you need to use adView? It's null and you will get a NullPointerException.

Stephen S
  • 166
  • 1
  • 1
  • 12
  • "If you look at your method header, you have a variable passed in called adView and you have a field called adView" <-- Sorry I couldn't get the mearning. If I do this under my method: [adView = new AdView(xxx);] <-- so am I directly refering to the field "adView" and not passing a variable? Since in order to passing variable, I have to do [AdView adView = new AdView(xxx);] instead – Kit Ng Apr 21 '14 at 20:10