Java Reference
In-Depth Information
public void printHiddenValue() {
// Declare a local variable value, which hides the value instance variable
int value = 131;
// Print value using simple name, which refers to the local variable - 131
System.out.println("value=" + value);
// Print value using keyword this, which refers to the instance
// variable value with value 828
System.out.println("this.value=" + this.value);
// Print value using keyword this qualified with the class name,
// which refers to instance variable value as 828
System.out.println("QualifiedThis.this.value=" + QualifiedThis.this.value);
}
}
Listing 2-18. Testing the Use of the Keyword this Qualified with the Class Name
// QualifiedThisTest.java
package com.jdojo.innerclasses;
public class QualifiedThisTest {
public static void main(String[] args) {
QualifiedThis qt = new QualifiedThis();
System.out.println("printValue():");
qt.printValue();
System.out.println("\nprintHiddenValue():");
qt.printHiddenValue();
}
}
printValue():
value=828
this.value=828
QualifiedThis.this.value=828
printHiddenValue():
value=131
this.value=828
QualifiedThis.this.value=828
You can refer to an instance variable in any of the following three ways, if its name is not hidden:
Using the simple name, such as
value
Using the simple name qualified with the keyword
this , such as this.value
Using the simple name qualified with the class name and the keyword
this , such as
QualifiedThis.this.value
 
Search WWH ::




Custom Search