Reputation: 86187
Following this tutorial:
http://developer.android.com/training/notepad/notepad-ex2.html
In Step 2, this method gets called:
registerForContextMenu(getListView());
which is a public method of Activity. Now, I'm a bit of a Java newbie here - I thought if you wanted to call an instance method of a superclass you needed to preface it with this. E.g.
this.registerForContextMenu(getListView());
Is it just a style thing here? Is there any difference between
this.registerForContextMenu
and simply
registerForContextMenu
Upvotes: 1
Views: 1036
Reputation: 3860
To call a method of superclass either you need object of superclss or keyword super . eg.
superObject.superclassMethod();
super.superclassMethod();
this is a reference of the current object. this can be used to call method of a class in which it is used. this can never be used to call a superclass method.
As for
this.registerForContextMenu()
and
registerForContextMenu()
no such difference. you can use either of them.
Upvotes: 1
Reputation: 75629
you do not have to use this
. If you ommit it it is assumed you called method in this
scope. One particular example when this
may help could be i.e.:
Boolean someVar;
public function setMe( Boolean someVar ) {
this.someVar = someVar;
}
In this case, w/o this
you would get the error.
Upvotes: 1
Reputation: 32391
Both ways are correct for calling a method on the current (this) instance of the class. Non private methods are inherited from super classes, so you can use the same syntax to call such methods.
Upvotes: 0
Reputation: 46219
No, there is no difference.
You don't have to use this.
, but it is often done anyway to make the code clearer.
For one thing, it makes it easy to tell if a method is static
or not if you use the convention of calling instance methods like this:
this.registerForContextMenu()
and static
methods like this:
ClassName.staticRegisterForContextMenu()
Upvotes: 2