Results 1 to 6 of 6
Thread: Thread static problem
- 11-15-2011, 11:45 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Thread static problem
Hello i am new to using threads.
i have a class which calls a method of another class, but i get the non-static method can not be called from a static context, error.
i have not declared anything static in the thread class.
so my question is, is threads always static, and is it true that everytime i want to call a method from another class in a thread, the method have to be declared static??
- 11-16-2011, 10:01 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Re: Thread static problem
Show the parts of the code in question.
- 11-16-2011, 10:42 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Thread static problem
Here is the code
The problem is the Skriv methodJava Code:import javax.swing.JPanel; import java.awt.*; public class TestBoard1 extends JPanel{ public TestBoard1(){ Runnable r = new Shuffler(); Thread t1 = new Thread(r); t1.start(); } public void Skriv(){ System.out.println("from thread"); } }//End TestBoard1 class Shuffler implements Runnable{ public void run(){ try{ while(true){ TestBoard1.Skriv(); Thread.sleep(500); } }catch(InterruptedException iex){} }//End run }//End Class
- 11-16-2011, 10:44 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Re: Thread static problem
TestBoard1 is a class name, not an instance, so you are trying to call an instance method with a static reference.
- 11-16-2011, 11:43 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Thread static problem
I have changed the code so a instance "tt" of testboard1 is created, but this just gives me another question.
Now it gives me the Cannot find symbol error of the intance tt when i try to compile the tt.Skriv() in shuffler.
why can't the shufller class not find the "tt" instance when itself was created by that instance??
Java Code:import javax.swing.JPanel; import java.awt.*; public class TestBoard1{ public TestBoard1(){ Runnable r = new Shuffler(); Thread t1 = new Thread(r); t1.start(); } public void write(){ System.out.println("from thread"); } public static void main(String [] s){ TestBoard1 tt = new TestBoard1(); } }//End TestBoard1 class Shuffler implements Runnable{ public void run(){ try{ while(true){ tt.write(); Thread.sleep(500); } }catch(InterruptedException iex){} }//End run }//End Class
- 11-16-2011, 11:47 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
Yet another "non-static method cannot be referenced fro a static context" problem
By jazzermonty in forum New To JavaReplies: 4Last Post: 03-14-2011, 12:05 AM -
Static problem
By rizowski in forum New To JavaReplies: 7Last Post: 01-26-2011, 03:43 PM -
Are static methods thread safe?
By sinisa.medic in forum Threads and SynchronizationReplies: 6Last Post: 06-04-2010, 02:17 PM -
Thread and Static
By vincent2001@gmail.com in forum New To JavaReplies: 2Last Post: 08-15-2008, 01:45 PM -
Local Variables for a static method - thread safe?
By mikeg1z in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks