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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:58:20+00:00 2026-06-13T17:58:20+00:00

Java newbie here. Here’s what I’d like to do: Enumerate over a list of

  • 0

Java newbie here. Here’s what I’d like to do:

  1. Enumerate over a list of classes, each of which extends the same superclass.
  2. Ask the class whether it’s interested in an event.
  3. If the class is interested, instantiate the class and call the object’s event handler.

The idea is that steps 2 and 3 will prevent instantiation of classes that aren’t interested. However, because I’m calling a method before instantiation, the check would have to be done statically. Java (rightly) doesn’t allow the overriding of static methods, so it seems that I have to instantiate the class in step 2, making the sequence look like this:

  1. Enumerate over a list of classes, each of which extends the same superclass.
  2. Instantiate each class and ask the object whether it’s interested in the event.
  3. If the object is interested, call its event handler. If it’s not interested, throw it away.

Am I missing a general way to accomplish the first set of steps?

Note that this question is mostly theoretical. Object creation overhead may be low enough to render it moot. I’m interested in the possibilities, though.

  • 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-13T17:58:21+00:00Added an answer on June 13, 2026 at 5:58 pm

    Since we’re speaking about theory, I’m pointing at some facts and speaking in terms of design.

    Static methods are not associated to a particular instance of a class, so overriding is not an option since it depends on having an instance. I’m talking about Java, because I recall some other languages that allow class method overriding.

    The workaround to this is to define a static method in each subclass that returns the events it is interested in, so you can know this data before instantiation.

    Another option is to put a specific class in charge of those objects instantiation and making that class keep a table associating an event with a list of interested classes (table that you can initialize and configure). This approach seems more maintainable because you won’t have to change code if you want to unsuscribe a class from an event.

    In the end, you just instantiate all the classes thata re associated to a certain event:

    public class EventClassCreator {
        private Map<String, List<String>> subscriptions;
    
        public EventClassCreator() {
            subscriptions = new HashMap<String,Set<String>>();
        }
    
        public void addSubscription(String event, String class) {
            if(subscriptions.containsKey(event))
                subscriptions.get(event).add(class);
            else {
                Set<String> subscriptionsForEvent = new HashSet<String>();
                subscriptionsForEvent.add(class);
                subscriptions.put(event, subscriptionsForEvent);
            }
        }
    
        //You just need to make an event that loops over the list of classes, 
        //checks a subscription and instantiates a class if it is in the 
        //proper list.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie here...can I write one program which incorporates .NET LINQ and also various Java
newbie doing Java homework here. I have one class named Album which contains the
Java newbie here. I am using a third party library which gives me a
Java newbie here. I'm trying to run the latest version of MWDumper , which
Java newbie here, I am trying to find the output of the following chunk
I am a Java newbie so my questions may look like an easy one.
Java newbie here, I'm having trouble setting a new line in this code: String
Java, ANTLR and Netbeans newbie here. I have installed a jdk and netbeans. I
I'm a java newbie looking for some help reading a file. Here is the
I'm writing code for a game currently in java (newbie programmer here), but I'm

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.