Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default Thread and Static

    Hi Everyone,

    This is my first post,thanks in advance for your help

    I was reading an example which explains the use of SwingUtilities, then I simply come up with two ideas which proved to be wrong...Can you help give me some explaination,thanks!

    The code is very simple..Creat a jframe ,inside it,there is jlabel, after one second the jlabel's text is changed;

    What confuses me is why declare "static test1 ssp;"

    import javax.swing.*;

    import java.util.*;
    import java.util.concurrent.TimeUnit;

    public class test1 extends JFrame{

    JLabel label;
    public test1(){
    super("hi");
    label=new JLabel("label");
    add(label);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,100);
    setVisible(true);

    }
    //look at the following line
    static test1 ssp;
    public static void main(String[] args) throws Exception{
    SwingUtilities.invokeLater(new Runnable(){
    public void run(){ssp=new test1();}}
    );
    TimeUnit.SECONDS.sleep(1);
    SwingUtilities.invokeLater(new Runnable(){


    public void run(){
    ssp.label.setText("hi"); }
    }
    );

    }
    }

    The above Code is the example..runs correctly.
    then I change the code like this ( no static test1 ssp any more)


    public static void main(String[] args) throws Exception{
    SwingUtilities.invokeLater(new Runnable(){
    public void run(){test1 ssp=new test1();}}
    );
    TimeUnit.SECONDS.sleep(1);
    SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    ssp.label.setText("hi"); }
    }
    );

    }
    }
    Now there is error message popping up saying that"SSP can't be resolved"

    I don't get this...ssp as object of test1 has been created in the first SwingUtilities 1 seconde before the seconde SwingUtilites..then why it can't resolve SSP? is this because of two threads?or....


    This way


    public static void main(String[] args) throws Exception{
    SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    test1 ssp=new test1();
    TimeUnit.SECONDS.sleep(1);
    ssp.label.setText("hi");}
    }
    );


    }


    then there is promting "unhandled exception for TimeUnit...."
    Last edited by vincent2001@gmail.com; 08-14-2008 at 04:41 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    ublic void run(){test1 ssp=new test1();}}
    );
    You define ssp as a variable that is local to the run method. NO code outside of the run method can see ssp.
    has been created in the first SwingUtilities 1 seconde before the seconde
    It looks like you are confusing compile time with run time. I assume that the error occurs during compilation. The 1 second before would be at runtime.

    When you get errors, please copy and paste them here. Don't edit them.

  3. #3
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks norm,

    you got the point! I confuse two things
    1) Compile time and run time
    2) Local variable inside Run() ...

    I thought everything inside Main() is in one scope....;(

    and thanks for telling me that I should copy and paste the error message here.That was my first post..

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 12:35 AM
  2. Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  3. Local Variables for a static method - thread safe?
    By mikeg1z in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 01:06 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •