Results 1 to 3 of 3
Thread: Thread and Static
- 08-14-2008, 04:38 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
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.
- 08-14-2008, 05:49 PM #2
You define ssp as a variable that is local to the run method. NO code outside of the run method can see ssp.ublic void run(){test1 ssp=new test1();}}
);
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.has been created in the first SwingUtilities 1 seconde before the seconde
When you get errors, please copy and paste them here. Don't edit them.
- 08-15-2008, 01:45 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
non-static member can not be referenced from a static context
By christina in forum New To JavaReplies: 3Last Post: 03-20-2009, 12:35 AM -
Explicit static initialization with the static clause
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:07 PM -
Local Variables for a static method - thread safe?
By mikeg1z in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 01:06 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks