Reputation: 359
If my class extends Activity
and implements SensorEventListener
, which one will this
refer to - Activity
or SensorEventListener
? or Both?
Thanks a lot.
public class MainActivity extends Activity implements SensorEventListener
Upvotes: 0
Views: 69
Reputation: 509
this
keyword represents current activity or class or object.
e.g.
private int number;
public example(int number) {
this.number = number;
}
here, this.number represents private int number variable and number is methods int number.
Another example would be (its the same as in java)
class example implements ActionListener{
public static void main(String[] args)
{
JButton button = new JButton();
button.addActionListener(this);
}
}
Since you have implemented the action listener to your class / project when you type this, it will call the action listener library.
Upvotes: 1
Reputation: 1699
this
refers to the current instance of the class. implements
is for an interface so this
can never refer to that. You cannot create an instance of an interface, only of classes. Activity
is the superclass of MainActivity
and is also not having an instance of it made. The instance will be of MainActivity
However, although this is a bit pointless, you can cast this
to all of MainActivity
, Activity
or SensorEventListener
without any problems.
Upvotes: 2
Reputation: 29912
this
will refer to MainActivity
that will be the instance of the class
Of course if you "change the contest" and look to an instance of Activity
: in that case this
could refer to Activity
but looking to your question, the answer is definitely MainActivity
Upvotes: 2