Hal Vaughan wrote:
I have a class that is sometimes called from the command line and sometimes
from another class.
When the user clicks "Quit" on the panel it opens, if it is running from the
command line, it quits. The problem is that I need a way to terminate the
class and the object without using quit, so it doesn't kill other classes.
The QUIT button calls an actionListener, so if I have "return" in the
listener, it closes the window, but the class is still active.
How can I return from the class (does that makes sense) and get the object
made from this class to "self destruct" without exiting the JVM?
It is a lot easier to discuss with concrete names, so let ServiceClass
be the class providing the service and ClientClass be the class invoking
ServiceClass.
Depending on your threading needs, try the following:
1) Register a callback object in the ServiceClass that it can invoke
when it is done,
2) Call notify() on an agreed upon object (perhaps even the instance of
ServiceClass itself) when ServiceClass is finished.
When the ServiceClass is finished, it notifies the ClientClass in one of
the above ways. Then you can clean up ServiceClass in the appropriate
manner. (E.g., call dispose() on Swing components, set all references
to ServiceClass to null, etc.)
Another potential solution is to use a modal dialog window. The thread
invoking show() on a modal dialog window will block until the window is
hidden. If this is appropriate for your environment, go for it.
Ray