Article View: borland.public.jbuilder.java.language
Article #15506Re: Singleton pattern impossible in Java?
From: Gemplus Software
Date: Wed, 02 Jun 1999 00:00
Date: Wed, 02 Jun 1999 00:00
100 lines
3369 bytes
3369 bytes
Hello Michael,
I try your example within Jbuilder3 Field Test3 without the pb you
encountered.
Hope this help !
Michael Boyle wrote:
> 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:
<3754F1AF.DBA106EB@gemplus.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<375401EA.A6FBEDCA@adopt.qc.ca>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,6d4ef91dc0571b30 X-Google-Attributes: gidf74b3,public From: Gemplus Software <gemplus.software@gemplus.com> Subject: Re: Singleton pattern impossible in Java? Date: 1999/06/02 Message-ID: <3754F1AF.DBA106EB@gemplus.com> X-Deja-AN: 484793664 Content-Transfer-Encoding: 7bit References: <375401EA.A6FBEDCA@adopt.qc.ca> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language