Java Interface - need help

I’m slightly confused.

I thougt in java you can’t create an object of an interface type . How is this possible because it doesnt make sense.

here is the code im confused with.

                    logIn.addActionListener(new ActionListener(){
		      public void actionPerformed(ActionEvent arg0) {
                                //do something
                          }

Thank you for all your help

Maybe this post back in 2005 may help

hooknc is right. Practical reason to use this is to avoid writing another Java file to implement 1 method.

That is called an Anonymous Inner Class.

The code that you see is doing the following…

Creating a new object of type ActionListener (which is an interface), but it is defining the behavior for the interface by implementing the actionPerformed method.

BTW, the use of Anonymous Inner Classes is great for exactly that type of example, handling events from the user interface.

I can understand your confusion but you should really treat this as a way to create Inner Class then treating as an Interface. In the history of Java, there was one release back in the days of Java 1.1 where they removed Anonymous Class. Argument was exactly what you said. However, the lazy Java programmer who already used it made an out cry and they put it back. Still, in realistic point of view Anonymous Class removes unnecessary Java files.