🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Help: static data member got reset...

#16691
From: Mark X. Yang
Date: Fri, 22 Jan 1999 00:00
92 lines
2859 bytes
I tried the following program:

import java.awt.*;
import java.awt.event.*;

public class Main {
   static MyObject MY_OBJ;

    static public void main(String[] args) {
     MY_OBJ = new MyObject();
        MyFrame frame = new MyFrame();
         frame.setVisible(true);
    }
}

class MyFrame extends Frame {
     Button myButton;
     MyFrame() {
           setSize(100,50);
      add(myButton = new Button("push"));
         myButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                     someMethod();
               }
         });
    }

    public Dimension getMinimumSize() {
         return new Dimension(100,50);
    }

    void someMethod() {
         Main.MY_OBJ.someMethod1();
    }
}

class MyObject extends Object {
     void someMethod1() {
        System.out.println("test");
     }
}

I compiled this using plain jdk1.1.6.
This program runs as expected (it prints out "test" on the console when the
button
is pressed). Maybe I
misunderstand your problem. Can you give me a more concrete  example?



John Yu <jyu@objectmastery.com> wrote in message
news:36A8031C.CC5DDA7@objectmastery.com...
>I experience this problem a couple of times. Please look at the
>following code segments:
>
>  public class Main {
>    static MyObject MY_OBJ;
>
>    static public void main(String[] args) {
>      MY_OBJ = new MyObject();
>      MyFrame frame = new MyFrame();
>      frame.setVisible(true);
>    }
>  }
>
>  public class MyFrame extends Frame {
>    //...
>    void someMethod {
>      Main.MY_OBJ.someMethod();  // Main.MY_OBJ becomes null here!
>      //...
>    }
>  }
>
>MyFrame.someMethod() is called to event a GUI event. I can't
>understand why Main.MY_OBJ becomes null at that point. Moreover, this
>is a random problem. Sometimes it works fine but sometimes doesn't.
>
>Is it a bug in the Java runtime?
>
>Or is it because that the main thread has ended and the Main *class*
>object got garbage collected? (This is a weird thought...)
>
>What complicates the problem more is if I use the debug feature in
>JBuilder to step thru the code, Main.MY_OBJ always retains its value
>and doesn't have a problem!
>
>I'm using JBuilder 2.01 and jdk116_borland.
>--
>John
>
>
Technical Details
Message-ID: <789bss$ilj1@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36A8031C.CC5DDA7@objectmastery.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,ad7c1cb07f7c3e7f
X-Google-Attributes: gidf74b3,public
From: "Mark X. Yang" <xyang@cs.ucla.edu>
Subject: Re: Help: static data member got reset...
Date: 1999/01/22
Message-ID: <789bss$ilj1@forums.borland.com>
X-Deja-AN: 435554498
References: <36A8031C.CC5DDA7@objectmastery.com>
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.0810.800
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.language