Java - What is the output: this keyword variable name

Question

What is the output of the following code?


class Student {
  void m1() {
    
    // Invoke the m2() method
    this.m2(); // same as "m2();"
  }

  void m2() {
    int this = 10;
    System.out.println(this);
    
  }

}

public class Main {
  public static void main(String[] args) {
    Student s = new Student();

    s.m1();
  }
}


Click to view the answer

// An error. Cannot name a variable this
int this = 10;

Note

this is a reserved keyword, you cannot use it as variable name

Related Quiz