1

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

4 Answers 4

2

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()
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.