File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Anonymous inner classes for event listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Anonymous inner classes for event listeners" Watch "Anonymous inner classes for event listeners" New topic

Anonymous inner classes for event listeners

K. Tsang

Joined: Sep 13, 2007
Posts: 2242

Hi all, I want to know how often do people separate their event listeners to be inner class or public class?

I'm thinking anonymous inner classes would make junior programmers harder to read for clarity sake. If public classes, then most likely I need to pass the necessary variables to the new class. If inner classes, then I need to make the necessary variables as instance variables.

Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 175
Ordinarily I would use anonymous inner classes for the event listeners. For SCJD I just used seperate classes as this seems so much easier for a junior programmer to understand.

Not sure why your event listeners need to be public though? All mine are private classes in the class where they are used. The variables required by these inner classes are setup as instance variables in the main class for use by the inner classes as you suggested

Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
K. Tsang

Joined: Sep 13, 2007
Posts: 2242

Thanks Kevin. The reason I want put listeners as public classes is that there are multiple places to do the same thing like disposing windows, or checking text field length.
jQuery in Action, 2nd edition
subject: Anonymous inner classes for event listeners
Similar Threads
firing an event when it has not been fired
Inner Classes when to use them?
separate Event Listener class
Inner class or methods
Dumb question about how javac names files