πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #15500

Re: Singleton pattern impossible in Java?

#15500
From: Axel Boness
Date: Tue, 01 Jun 1999 00:00
114 lines
4291 bytes
I have experienced the same kind of problem.

In "init" I setted a static member of a class and when running later on
(via RMI calls) the static singleton member was back to null !!!

Only solution I found for now on is to overqualify the static member by
indexing the enclosing class name.

So in your example change references to "app" in references to
"TestApp.app"

Atonishing !!! or not ? 

Michael Boyle a οΏ½crit :
> 
> 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);
>     }
>   }
> }

-- 
+-------------+-------------------------------------------------------+
| Axel Boness |            mailto:Axel.Boness@Akazi.com               |
+-------------+-------------------------------------------------------+
|                       Akazi Technologies SA                         |
| Voice : (33) 4 76 04 15 91              Fax : (33) 4 76 04 15 92    |
+---------------------------------------------------------------------+
Technical Details
Message-ID: <3754078A.CFA8AC1A@Akazi.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <375401EA.A6FBEDCA@adopt.qc.ca>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f74b3,6d4ef91dc0571b30
X-Google-Attributes: gidf74b3,public
From: Axel Boness <Axel.Boness@Akazi.com>
Subject: Re: Singleton pattern impossible in Java?
Date: 1999/06/01
Message-ID: <3754078A.CFA8AC1A@Akazi.com>
X-Deja-AN: 484511512
Content-Transfer-Encoding: 8bit
References: <375401EA.A6FBEDCA@adopt.qc.ca>
X-Accept-Language: fr
Content-Type: text/plain; charset=iso-8859-1
X-Complaints-To: abuse@oleane.net
X-Trace: minus.oleane.net 928253820 26588 212.124.1.125 (1 Jun 1999 16:17:00 GMT)
Organization: Akazi Technologies SA
Mime-Version: 1.0
Reply-To: Axel.Boness@Akazi.com
NNTP-Posting-Date: 1 Jun 1999 16:17:00 GMT
Newsgroups: borland.public.jbuilder.java.language