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

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