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'

Comments