Using the following as an example, the ButtonExample class contains a JButton. The ActionListener for the JButton is an anonymous class.
Within the actionPerformed() procedure of the anonymous class there is a call to otherProcedure(); which is part of the ButtonExample class and separate from the JButton itself.
Why is this anonymous class allowed to make reference to a method is a completely separate object? Is this related to Inner/Outer Class behavior? Is this the general rule when using anonymous classes? Are anonymous classes Inner classes or something else?
import java.awt.event.*;
import javax.swing.*;
public class ButtonExample
{
public ButtonExample()
{
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
tf.setText("Hello World");
otherProcedure();
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args)
{
ButtonExample be = new ButtonExample();
}
private void otherProcedure()
{
System.out.println("Other Procedure");
}
}