Skip to main content

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

One of my favourite frameworks is webfirmframework

Forms are used everywhere in a web/desktop/other applications. In these forms, the user may enter values starting and/or ending with spaces. But we may not need theses spaces at the starting and ending, so we may want to trim it. As we know, we may simply use a trim() method to trim a string. But, what if we want to trim all strings in a bean/pojo/value object. Suppose, consider a form having some fields, first name, last name, address etc... The user may enter the values as

First Name :

Last Name :

Address :  House name, Place, Distring

The values have spaces in the beginning and ending. So, to write code to trim every string separately is a kind of messing sometimes.

Here is a method which can trim all strings in a bean/pojo/value object (Only the strings will be trimmed, so it doesn't matter if it contains data types other than strings). Before that let us make a value object (POJO/bean) for these fields.
Consider …

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

There are lot of JavaScript libraries which make the development easier. But when we use a pure JavaScript we understand the core functionality & logic of a JavaScript engine. When we use a new js library we have to learn its own functions, features and their usages and they might also lead to a misconception of JavaScript execution, eg: AngularJS. However, some functionalities like charts/diagrams may not be able to do in a shorter time with pure JavaScript, in such cases we have to rely on third party libs.

When we use third party js libraries which provide ready made ui features we might face some unsolvable issues when customization required for its ui features so if we have a plan for nth level of ui customization then it's always better to use pure JavaScript. Modifying the js library itself is not always safe.

Which is the best framework for generating html from java code

Are you looking for answers of any of the following questions

Building HTML in Java code onlyBetter Html Builder in javaBuilding HTML in Java code onlyHTML code in Java classIs it a good practice to write Java methods to return HTML strings that will be returned to the display/presentation layerWhich is the best opensource java library/jar for html reportingServer controlled UI framework in JavaIs there any Java framework which is faster than SSD Then please have a look at webfirmframework. When you can well maintain the code it's always fine to return html by even a method.

Buy me a chocolate if I'm the first one brought it in your attention!