Why is Class.this (ClassName.this) used?

//What is Class.this

package tcase;

 * @author visruth cv
public class First {

    final String visru;
    final Second second;

    First(String visru) {
        this.visru = visru;
        this.second = new Second(visru + "-second");

    class Second {

        final String visru;
        final Third third;

        Second(String visru) {
            this.visru = visru;
            this.third = new Third(visru + "-third");

        class Third {

            final String visru;
            final Fourth fourth;

            Third(String visru) {
                this.visru = visru;
                this.fourth = new Fourth(visru + "-fourth");

            class Fourth {

                final String visru;

                Fourth(String visru) {
                    this.visru = visru;

                void toPrint() {
                    System.out.println("By Current Obect: " + this.visru);
                    System.out.println("First: " + First.this.visru); // current object of class First
                    System.out.println("Second: " + Second.this.visru); // current object of class Second
                    System.out.println("Third: " + Third.this.visru); // current object of class Third
                    System.out.println("Fourth: " + Fourth.this.visru); // current object of class Fourth

                    System.out.println("below prints addresses");

                    System.out.println("Address of current Obect: " + this);
                    System.out.println("Address of First: " + First.this); // current object of class First
                    System.out.println("Address of Second: " + Second.this); // current object of class Second
                    System.out.println("Address of Third: " + Third.this); // current object of class Third
                    System.out.println("Address of Fourth: " + Fourth.this); // current object of class Fourth

    static public void main(String[] args) {      
        final First first = new First("first");

This gives output as follows (printing addresses may vary in each execution).

By Current Obect: first-second-third-fourth
First: first
Second: first-second
Third: first-second-third
Fourth: first-second-third-fourth
below prints addresses
Address of current Obect: tcase.First$Second$Third$Fourth@15f5897
Address of First: tcase.First@b162d5
Address of Second: tcase.First$Second@1cfb549
Address of Third: tcase.First$Second$Third@186d4c1
Address of Fourth: tcase.First$Second$Third$Fourth@15f5897

It's a good example....
`Class.this` means that the current object is referred from that particular class.
`First.this` means the current object of the class First.
`Second.this` means the current object of the class Second.
`Third.this` means the current object of the class Third and so on.
And also, see the relation of all addresses.
The two objects (given as bold above) are having the same memory addresses (15f5897).
Why it happened so,??., because
the method toPrint is called under the reference of Class Fourth. Therefore, the current object at the point is the object of the class Fourth. So,
these two lines
System.out.println("By Current Obect: " + this.visru); and
System.out.println("Address of Fourth: " + Fourth.this);
print the same output as 'first-second-third-fourth' in those prints.

if anybody has the doubts, please comment on the post.....


  1. sir ,thanku vry much 4 a kind f diffrnt invention...that i havnt seen in my carear.

  2. Thank you sir this is very helpful .I am expecting more this kind of interesting techniques...


Post a Comment

Popular posts from this blog

How to trim all strings in a bean (pojo/value object) in Java

Which is the best framework for generating html from java code

Why Should You Use Pure JavaScript for UI instead of third party js libraries