Hi. I'm just starting to learn about inner classes. The following code is an example I followed from my book, but I still don't fully understand the explanation on lines 22 - 24:
package samsExperiments; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class InnerClassTest extends JFrame{ public InnerClassTest() { //code that sets up the frame this.setTitle("TestFrame"); this.setSize(400,100); this.setLocationByPlatform(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); this.add(panel); //code that creates the button and adds the listener JButton button1 = new JButton("Click me!"); //Next, we create an object from the inner ClickListener class and assign it //to an ActionListener variable. This is possible because the ClickListener class //implements the ActionListener interface and it's actionPerformed method: ActionListener listener = new ClickListener();//ClickListener cannot be resolved to a type button1.addActionListener(listener); //display the frame window panel.add(button1); this.setVisible(true); //the inner class that implements the listener class ClickListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { System.out.println("The button was clicked!"); } }//end of the inner class }//end of the outer class }
The error on line 25 says, "ClickListener cannot be resolved to a type". I'm used to seeing an object instantiation be created from a class such as "class x = new class()".
Since when is it possible assign an object instance to a variable instead of creating one from a class? What is going on here?