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

  • SEARCH
  • Home
  • 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 8666661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:47:08+00:00 2026-06-12T17:47:08+00:00

Possible Duplicate: Anonymous vs named inner classes? – best practices? When working with Java

  • 0

Possible Duplicate:
Anonymous vs named inner classes? – best practices?

When working with Java Handlers in general, people often use three approaches :

  1. Create a class that will implements all needed interfaces
  2. Create a anonymous inner class
  3. Create a local class

I’m interested only about the differences between 2) and 3)

Comparing 2) to 3) we can consider the following code. In this example, only one class will be generated by the Compiler.

class MyHandler implements ClickHandler, DragHandler, MovedHandler
    {
        public void onClick(ClickEvent clickEvent)
        {
            // Do stuff
        }

        public void onMoved(MovedEvent movedEvent) {
            // Do stuff
        }

        public void onDrag(DragEvent event) {
            // Do stuff
        }
    }

    MyHandler localHandler = new MyHandler();
    button.addClickHandler(localHandler);
    something.addDragHandler(localHandler);
    that.addMovedHandler(localHandler);

In the following example, three inner classes will be generated by the Compiler (correct me if I’m wrong).

button.addClickHandler(new ClickHandler()
    {
        public void onClick(ClickEvent clickEvent)
        {
            // Do stuff
        }
    });
something.addDragHandler(new DragHandler()
    {
        public void onDrag(DragEvent event)
        {
            // Do stuff
        }
    });
that.addMovedHandler(new MovedHandler()
    {
        public void onMoved(MovedEvent movedEvent)
        {
            // Do stuff
        }
    });

My question is : Is there any other difference between these two approaches ? Is there any caveats of using one despite the other ?

  • 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-06-12T17:47:09+00:00Added an answer on June 12, 2026 at 5:47 pm

    The only difference is that the classes in example 1 are named and the classes in example 2 are anonymous. Functionally they are the same otherwise.

    If you instead declared the class like

    static class MyHandler implements ClickHandler ...
    

    Then what you would have would be a static nested class which differs from the inner class in that the former does not have a reference or direct access to the methods of the enclosing class.

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

Sidebar

Related Questions

Possible Duplicate: How are Anonymous (inner) classes used in Java? i have a question
Possible Duplicate: Double brace initialisation (anonymous inner class) with diamond operator Using Java 7,
Possible Duplicate: WHY an Anonymous class in Java can't implement multiple interfaces directly? Simply
Possible Duplicate: What are the benefits to using anonymous functions instead of named functions
Possible Duplicate: Anonymous code blocks in Java I learned the other day (on SO)
Possible Duplicate: LINQ to SQL: Return anonymous type? I have a standard LINQ to
Possible Duplicate: JavaScript: Why the anonymous function wrapper? I would like to ask you
Possible Duplicate: VB.NET RemoveHandler & Anonymous Methods If I add an event handler like
Possible Duplicate: What is the purpose of wrapping whole Javascript files in anonymous functions
Possible Duplicate: JavaScript: Why the anonymous function wrapper? Sometimes I see a structure like

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.