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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:07:23+00:00 2026-06-14T21:07:23+00:00

In Java, I can say Thread t = new Thread(){ public void run(){ //

  • 0

In Java, I can say

Thread t = new Thread(){
    public void run(){ // do stuff }
}

(or something much like that) to declare an anonymous class inline. This is most useful for making event handlers or other callback sorts of things, that in any sensible language wouldn’t require an object to own them in the first place.

I face the same problem in Jython — I want to define an event handler, but I don’t want to build a whole standalone class to do so.

Ideally, I’d just be able to pass a lambda and be done with it, but if this is possible, I can’t find it anywhere in the docs.

  • 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-14T21:07:25+00:00Added an answer on June 14, 2026 at 9:07 pm

    Here are the background-reading links from my original answer:

    Read this: Does Python have something like anonymous inner classes of Java?

    Then, read this: Does Python have anonymous classes?

    Finally, read this: Is it possible to create anonymous objects in Python?

    [IMPROVEMENT TO ORIGINAL ANSER]

    Jython implicitly converts a function to an event handler when appropriate. So, you can write this inside of one of your methods:

    def _(self, event):
        print 'Cancel button was clicked!'
        if self.task is not None:
            self.task.cancel()
    cancelButton.setOnAction(lambda event: _(self, event))
    

    This approach provides three of desirable characteristics of an anonymous inner class.

    (1) It is highly localized. The handler code is next to the code that assigns the handler.

    (2) It is self aware, meaning you have self and can access to all of the members of the containing object.

    (3) It is (almost) anonymous. Python forces me to choose a name for multi-line function, but it also allows me to reuse that name. So, I can define as many local functions called _ as long as I use them right away. (Defining a second function by the same name makes the first function inaccessible by that name.) So, I am not forced to invent unique names ad-infinitum.

    You can also package this pattern in a decorator, which I think clarifies the intent a bit:

    @LocalEventHandler(self, cancelButton.setOnAction)
    def _(self, event):
        print 'Cancel button was clicked!'
        if self.task is not None:
            self.task.cancel()
    

    The decorator implementation looks like this:

    class LocalEventHandler(object):
        '''This decorator turns a local function into a *self aware* event handler.'''
    
        def __init__(self, context, assignHandlerFunc):
            self.context = context
            self.assignHandlerFunc = assignHandlerFunc
    
        def __call__(self, handler):
            self.assignHandlerFunc(lambda event: handler(self.context, event))
            return handler
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Java Threads and synchronization. Lets say I have: public class MyClass(){
The Java tutorials say that creating a Thread is expensive. But why exactly is
I have a java SE project or you can say applet , but i
How can I load a .java file into a CompilationUnit? For example, lets say
Is there anything simple Java can't do that can be done in a similar
I want to learn to write a thread pool in Java Can anyone point
First of all, I must say that I am quite new to the API
*I'm using Java. I have this thread, agent, that explores a room to determine
Let me first say that I have quite a lot of Java experience, but
In Java can I create a URI for a file located locally in the

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.