Using the this Keyword to Refer to an Instance Variable Whose Name Is Hidden by a Local Variable - Java Object Oriented Design

Java examples for Object Oriented Design:Field

Description

Using the this Keyword to Refer to an Instance Variable Whose Name Is Hidden by a Local Variable

Demo Code

public class Main {
  int num = 2017; // An instance variable

  void printNum(int num) {
    System.out.println("Parameter num: " + num);
    System.out.println("Instance variable num: " + this.num);
  }/*from   ww  w  .j  a  v a 2 s  . c  om*/

  public static void main(String[] args) {
    Main a = new Main();
    a.printNum(2000);
  }
}

Result


Related Tutorials