0

I do understand that Class.this used in case of nested classes but I came across a code which is using it without any nested classes.

// Find the View that shows the numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);

// Set a click listener on that View
numbers.setOnClickListener(new View.OnClickListener() {
   // The code in this method will be executed when the numbers View is clicked on.
   @Override
   public void onClick(View view) {
       Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
       startActivity(numbersIntent);
   }
});

In the line:

 Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);

Why is MainActivity.this used and why can't we use this.

Vamsi Rao
  • 27
  • 5

1 Answers1

3

Because here new View.OnClickListener() is anonymous inner class.

Pushpendra
  • 2,791
  • 4
  • 26
  • 49