Results 1 to 6 of 6
  1. #1
    Mira1000 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default 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??

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Thread static problem

    Show the parts of the code in question.

  3. #3
    Mira1000 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Thread static problem

    Here is the code

    Java 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
    The problem is the Skriv method

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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.

  5. #5
    Mira1000 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default 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

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Thread static problem

    Because tt is local to the main method. Add a constructor to your Shuffler class to pass it an instance of the TestBoard1 class and assign that instance to instance variable in the Shuffler class, then use that instance variable in the run method.

Similar Threads

  1. Replies: 4
    Last Post: 03-14-2011, 12:05 AM
  2. Static problem
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 01-26-2011, 03:43 PM
  3. Are static methods thread safe?
    By sinisa.medic in forum Threads and Synchronization
    Replies: 6
    Last Post: 06-04-2010, 02:17 PM
  4. Thread and Static
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-15-2008, 01:45 PM
  5. Local Variables for a static method - thread safe?
    By mikeg1z in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 01:06 AM

Posting Permissions

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