Wednesday, 25 July 2012

Example for Outer class reference

/*
 * This is a real time example for out class reference
 *
 */
package tcase;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author visruth cv
 */
public class Outer extends JFrame {

    private String outerClassField = "some value1";

    public Outer() {
        super("title of the frame");
        setSize(600, 600);
        setLayout(new FlowLayout());
        JButton b = new JButton("Ok");
        b.addActionListener(new ActionListener() {
            private String outerClassField = "some value2";
            public void actionPerformed(ActionEvent e) {
                System.out.println(Outer.this.outerClassField); //line 29
                System.out.println(outerClassField); //line 30
            }
        });
        add(b);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Outer();
    }
}
The line no.29 gives the current reference of outer class Outer.
Line no. 29 prints as 'some value1' and line no. 29 prints as 'some value2'

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");
        first.second.third.fourth.toPrint();
    }
}



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.
Here,
`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.....