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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:07:24+00:00 2026-05-17T20:07:24+00:00

Is there a feasible way to get my own code run whenever any class

  • 0

Is there a feasible way to get my own code run whenever any class is loaded in Java, without forcing the user explicitly and manually loading all classes with a custom classloader?

Without going too much into the details, whenever a class implementing a certain interface read its annotation that links it with another class, and give the pair to a third class.

Edit: Heck, I’ll go to details: I’m doing an event handling library. What I’m doing is having the client code do their own Listener / Event pairs, which need to be registered with my library as a pair. (hm, that wasn’t that long after all).

Further Edit: Currently the client code needs to register the pair of classes/interfaces manually, which works pretty well. My intent is to automate this away, and I thought that linking the two classes with annotations would help. Next, I want to get rid of the client code needing to keeping the list of registrations up to date always.

PS: The static block won’t do, since my interface is bundled into a library, and the client code will create further interfaces. Thus, abstract classes won’t do either, since it must be an interface.

  • 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-17T20:07:25+00:00Added an answer on May 17, 2026 at 8:07 pm

    If you want to base the behavior on an interface, you could use a static initializer in that interface.

    public interface Foo{
    
        static{
            // do initializing here
        }
    
    }
    

    I’m not saying it’s good practice, but it will definitely initialize the first time one of the implementing classes is loaded.

    Update: static blocks in interfaces are illegal. Use abstract classes instead!

    Reference:

    • Initializers (Sun Java Tutorial)

    But if I understand you right, you want the initialization to happen once per implementing class. That will be tricky. You definitely can’t do that with an interface based solution. You could do it with an abstract base class that has a dynamic initializer (or constructor), that checks whether the requested mapping already exists and adds it if it doesn’t, but doing such things in constructors is quite a hack.

    I’d say you cleanest options are either to generate Code at build time (through annotation processing with apt or through bytecode analysis with a tool like asm) or to use an agent at class load time to dynamically create the mapping.


    Ah, more input. Very good. So clients use your library and provide mappings based on annotations. Then I’d say your library should provide an initializer method, where client code can register classes. Something like this:

    YourLibrary.getInstance().registerMappedClasses(
        CustomClass1.class,
        CustomClass2.class,
        CustomClass3.class,
        CustomClass4.class
    )
    

    Or, even better, a package scanning mechanism (example code to implement this can be found at this question):

    YourLibrary.getInstance().registerMappedClassesFromPackages(
        "com.mycompany.myclientcode.abc",
        "com.mycompany.myclientcode.def"
    )
    

    Anyway, there is basically no way to avoid having your clients do that kind of work, because you can’t control their build process nor their classloader for them (but you could of course provide guides for classloader or build configuration).

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

Sidebar

Related Questions

Is there any feasible way of using generics to create a Math library that
There are a few ways to get class-like behavior in javascript, the most common
There are several types of objects in a system, and each has it's own
Short version: Is it easy/feasible/possible to program modal window in Flash (AS3)? Is there
I am using sandcastle to build my help file. Is there a way to
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There is previous little on the google on this subject other than people asking
There seem to be many ways to define singletons in Python. Is there a

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.