Article View: borland.public.jbuilder.java.language
Article #15499Singleton pattern impossible in Java?
From: Michael Boyle
Date: Tue, 01 Jun 1999 00:00
Date: Tue, 01 Jun 1999 00:00
92 lines
2988 bytes
2988 bytes
I have been unsuccessfully attempting to implement the singleton
pattern in my project. A short example demonstrating the problem
appears below. My environment is JBuilder 2.01 with
JDK1.1.6_Borland.
I have a class (see "TestApp") below, which stores a reference
to its sole instance as a private static data member.
The main() function causes this static member to be initialized,
and its constructor creates and displays a window with a
small menu.
After hitting the menu buttons a few times, you will see (I hope)
that THE FINALIZE METHOD FOR TestApp IS CALLED!!!!
Now, I realize that no object is storing a reference to my
TestApp instance, but surely any static member object of a
class should not be garbage collected.
What's going on here? Is this a JBuilder problem, a JDK problem,
or a "feature" of Java? If in fact static class members can
be arbitrarily garbage collected, then how can the Singleton
pattern be implemented in Java?
I'd be interested to know if this occurs with other versions
of JBuilder or the JDK.
//-----------------------------------------------------
// file TestApp.java
import java.awt.event.*;
import com.sun.java.swing.*;
public class TestApp {
private static TestApp app = null;
private TestApp() {
TestWin w = new TestWin();
}
protected void finalize() {
JOptionPane.showMessageDialog(null, "TestApp finalized");
}
public static TestApp getInstance() {
if (app == null) {
app = new TestApp();
}
return app;
}
public static void main(String[] args) {
TestApp.getInstance();
}
}
//-----------------------------------------------------
// file TestWin.java
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class TestWin extends JFrame {
public TestWin() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.getContentPane().setLayout(new BorderLayout());
this.setSize(new Dimension(400, 300));
this.setTitle("Singleton Test");
JMenu menu = new JMenu("File");
menu.add(new JMenuItem("Button 1"));
menu.add(new JMenuItem("Button 2"));
JMenuBar mb = new JMenuBar();
mb.add(menu);
this.setJMenuBar(mb);
validate();
setVisible(true);
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
Technical Details
Message-ID:
<375401EA.A6FBEDCA@adopt.qc.ca>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,6d4ef91dc0571b30,start X-Google-Attributes: gidf74b3,public From: Michael Boyle <michaelb@adopt.qc.ca> Subject: Singleton pattern impossible in Java? Date: 1999/06/01 Message-ID: <375401EA.A6FBEDCA@adopt.qc.ca> X-Deja-AN: 484503920 Content-Transfer-Encoding: 7bit Organization: Another Netscape Collabra Server User Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language