Which is preferable Sample.this.display() or this.display()?
class Sample{
void display(){
System.out.println("display() called");
}
void callDisplay(){
Sample.this.display(); // 1
this.display(); // 2
}
public static void main(String args[]){
Sample s = new Sample();
s.callDisplay();
}
}
- Can You explain the difference?
- Which is better choice?
- Is there any special meaning/purpose for Sample.this.display()?
The reason you could use the classname like
Sample.this.display()is when you are in an inner class and you want to referencethisof an enclosing class. In the example provided, it makes no difference.