Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:39:04+00:00 2026-05-16T03:39:04+00:00

In all the Java source code examples I have looked at the listeners have

  • 0

In all the Java source code examples I have looked at the listeners have always been declared in inner classes.

Why – what is the reason for coding the classes like this instead of having the listener(s) in their own seperate *.java file \ class?

Would having seperate classes for the listeners be considered a bad design?

If it’s not a bad design \ sackable offence could someone please post a short example demonstrating how to implement this?

Thank for reading.

Edit\Update – 10.8.2010:
Thanks to all who took the time to reply. Lots of insightful points to consider.
Having read all the answers I think that unless there is a very good reason for doing otherwise it is better and easier to declare listeners as inner classes.

Apologies for not coming back to this question sooner, but I don’t always have as much time for coding as I’d like 🙁

Happy coding.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:39:05+00:00Added an answer on May 16, 2026 at 3:39 am

    Good reasons for using inner classes:

    • Avoids adding extra top-level classes to your package namespace
    • Keeps the code locally encapsulated (e.g. within the component that requires the event handling behaviour)
    • static inner classes work well when they do not need to access fields of the enclosing class

    Possible reasons for using top level classes:

    • You have a special type of listener that you expect to be used by external packages or forms part of your API (e.g. in the Java class library itself)
    • The listener is used in many places, and is not logically specific to any one of many possible enclosing classes

    In short: inner classes are usually preferred, but if you have good reasons then it can be perfectly sensible to create top level classes instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Eclipse JDT library to parse java source code to visit all methods
I have a Java project with all source files in the src folder. This
Possible Duplicate: USD Currency Formatting in Java All- I have a small problem in
In my grails project I'm moving all calculations to java classes. Here is the
I would like to web-scrape the html source code of java-script pages that I
I have a program where some Java classes are available. It is possible to
I code mostly in Java and use Maven for all of my projects. I
I have some triangle polygons and drawing them in a traditional way: (android-java code)
I have been trying there last days to extend the default editor (java, xml,
I work on an open-source Java project, and we have a lot of resource

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.