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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:52:00+00:00 2026-05-25T22:52:00+00:00

I have a simple Screen class in C# that has a bunch of events

  • 0

I have a simple Screen class in C# that has a bunch of events (with corresponding delegates) like the FadeOutEvent.

I want to port my library to Java, and I find that the mechanism for events/delegates is really cludgey. Specifically, I cannot easily write code like:

if (someVar == someVal) {
  this.FadeOutComplete += () => {
    this.ShowScreen(new SomeScreen());
  };
} else {
  this.FadeOutComplete += () => {
    this.ShowScreen(new SomeOtherScreen());
  };
}

For all you Java-only guys, essentially, what I’m whinging about is the inability to reassign the event-handling method in the current class to something else dynamically, without creating new classes; it seems that if I use interfaces, the current class must implement the interface, and I can’t change the code called later.

In C#, it’s common that you have code that:

  • In a constructor / early on, assign some event handler code to an event
  • Later during execution, remove that code completely
  • Often, change that original handler to different handler code

Strategy pattern can solve this (and does), albeit that I need extra classes and interfaces to do it; in C#, it’s just a delcarative event/delegate and I’m done.

Is there a way to do this without inner/anonymous classes?

Edit: I just saw this SO question, which might help.

  • 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-25T22:52:00+00:00Added an answer on May 25, 2026 at 10:52 pm

    Most of the time, it’s done the other way round:

    this.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (someVar == someVal) {
                showSomeScreen();
            }
            else {
                showSomeOtherScreen();
            }
        }
    });
    

    But you could do something similar to your C# code by delegating to two other objects:

    private Runnable delegate;
    
    // ...
    this.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            delegate.run();
        }
    });
    
    // ...
    if (someVar == someVal) {
        this.delegate = new Runnable() {
            @Override
            public void run() {
                showSomeScreen();
            }
        };
    }
    else {
        this.delegate = new Runnable() {
            @Override
            public void run() {
                showSomeOtherScreen();
            }
        };
    }
    

    Delegates were proposed by Microsoft for Java a long long time ago, and were refused by Sun. I don’t remember if anonymous inner classes already existed at that time or if they were chosen as the alternative.

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

Sidebar

Related Questions

If I have a simple class that has a rectangle: package { import flash.display.Sprite;
If I have a Customer class that just has simple properties (e.g. Name ,
I have a program with a screen that has about 50 buttons, it looks
I have an application that contains a main Activity, which has a very simple
I have a simple application that starts with a splash screen activity that plays
I have a simple screen created in xml, the parent layout is a relativelayout
I have a simple app with a full screen UIWebView. This contains HTML generated
I have a simple graphics application which draws some stuff on the screen. Before,
I have a simple OpenGL application where I have 2 objects displayed on screen:
I have a simple gateway listener which generates a log at the screen output

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.