Anonymous vs. Regular Inner Classes

Inner classes are useful for instantiating and registering listeners on event sources. When should you use an anonymous inner class and when should you use a regular inner class? If you need only one listener of a given type to be registered on a single source, you should use the anonymous form. However, if you need to register listeners of a given type on multiple sources, using the anonymous form requires you to duplicate code. You must provide code to define and register the listener on each source. Also, every listener will be a different object whether that is what you want or not.

In the case of multiple sources, using a regular (inner) listener class usually eliminates duplicate code. Also, you can register different listener objects on each source, or you can share a single listener object among multiple sources. This program uses the anonymous form to register a window listener on the JFrame object, and uses the regular form to instantiate a single listener object and register it on ten separate buttons:

 import java.awt.*;import java.awt.event.*;import com.sun.java.swing.*;public class InnerClasses02 extends JFrame {public static void main(String args[]) {	new InnerClasses02();}//end main()//------------------------------------------------//  InnerClasses02() {//constructor    	JButton theButtons[] = new JButton[10];	RegInnerClass actionListener = new RegInnerClass();	for(int cnt = 0; cnt < 10; cnt++){		theButtons[cnt] = new JButton("" + cnt);		theButtons[cnt].addActionListener(actionListener);		getContentPane().add(theButtons[cnt]);	}//end for loop	setTitle("Inner Classes");	getContentPane().setLayout(new FlowLayout());	setSize(300,100);	setVisible(true);    	// Anonymous inner class to terminate the	// program when the JFrame is closed.	addWindowListener(new WindowAdapter() {		public void windowClosing(WindowEvent e) {			System.exit(0);}});//end WindowListener}//end constructor//=====================================================//  //Regular inner class ActionListener for buttonsclass RegInnerClass implements ActionListener{	public void actionPerformed(ActionEvent e){		System.out.println(e.getSource());	}//end actionPerformed()}//end RegInnerClass    }//end class InnerClasses02
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS