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 1025799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:58:58+00:00 2026-05-16T11:58:58+00:00

I would like to ask what is the good practice on using anonymous classes

  • 0

I would like to ask what is the good practice on using anonymous classes vs. named inner classes?

I am writing an Android application, which includes many UI elements (buttons, text fields, etc). For many of them I need some kind of listeners, so in onCreate of the application I have bunch of quite small anonymous classes like:

someButton.setOnClickListener(
    new View.OnClickListener() {
        public void onClick(View v) {
            // do something...
        }
    }
);

Each of of such anonymous class is 5 – 20 lines big – small enough and fits good for recommendations from Java™ in a Nutshell book:

In general, you should consider using an anonymous class instead of a local class if:

  • The class has a very short body.
  • Only one instance of the class is needed.
  • The class is used right after it is defined.
  • The name of the class does not make your code any easier to understand.

But the problem, IMO, is that onCreate becomes quite big and the code becomes more complex to read and understand by quick looking in to it. It is still easy to understand, but simply too big.

So what would be better practice in such case – have bunch of small inner sub-classes, where each of it is nicely separated , but used just once or better keep using anonymous classes instead?

  • 1 1 Answer
  • 3 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-16T11:58:59+00:00Added an answer on May 16, 2026 at 11:58 am

    I don’t think there is a clear answer one way or the other. Both styles work fine, its really just what you prefer.

    Another option is to have

    the contents of each onClick by a single function call, which will keep the anonymous classes very short. I.e:

    someButton.setOnClickListener(
        new View.OnClickListener() {
            public void onClick(View v) {
                doSomeButtonClick();
            }
        }
    );
    
    
    private void doSomeButtonClick() {
      // do something
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask whether or not is a good practice, to hold
Good morning! I would like to ask some help from you guys on how
I would like ask if there's a way to download an android layout from
I would like to ask such question, I have XML xsd`s, which generate beans
I've written a similar question which was closed I would like to ask not
I would like to develope a databased GUI application for free, which is cross-platform.
Good day! My regular expression is really bad and I would like to ask
I would like to ask if someone knows any good gem for creating nice
I would like to ask you for help, since my approach was not good.
I'm not really good with licenses thats why I would like to ask here

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.