What is the difference between Class.this and this in Java

In this case, they are the same. The Class.this syntax is useful when you have a non-static nested class that needs to refer to its outer class’s instance.

class Person{
    String name;

    public void setName(String name){
        this.name = name;
    }

    class Displayer {
        String getPersonName() { 
            return Person.this.name; 
        }

    }
}

Leave a Comment