This question is not about a specific program. This is about design - the UML phase of the project.

I am supervising a Java project. It will contain a lot of modal popup dialogs. From a C/C++ standpoint, I have a reference to the main window maintained throughout the program so every dialog may, as necessary, block interactivity with the main dialog. In Java, is there a standard way to do this?

This is a scenario: The main executable owns a JFrame that is the main window. The main executable has an object, I will call it a House. The House has some Room objects. The Room object has a Closet object.

If I want the Closet object to pop up a dialog, blocking input to the main window, do I have to send an instance of the main JFrame to the House, pass it along to each Room, then passing it along to each Closet? Is there a way to say "Give me the top level component"? Or... Is this simply not the way that it is done properly in Java?

1 Answer 1


Sure, if a data sink in your application's world really is unique, i.e. all messages of some kind originating anywhere in your system should end up there, then there's nothing wrong with having a dedicated instance servicing them all (although it might become a bottleneck with respect to performance or even deadlock).

It's just not customary to hold such a reference in a global variable. Much more often you see a singleton with a getInstance() method, a named entity that every component can look up via some sort of service registry, something invisibly weaved into the program by AOP, or similar mechanisms.

  • I will suggest that they use a singleton as there will only be one instance of the main class.
    – kainaw
    Sep 23, 2014 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.