Illegical static declaration in inner class

前端 未结 2 613
执念已碎
执念已碎 2021-01-15 18:40
import java.lang.*;

class mythread implements Runnable {

    Thread t1;
    String name = \"\";

    mythread(String thname){

        name = thname;
        t1 =          


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-15 19:21

    As per the error message, inner class cannot access the static variables;

    either remove the class t or declare it static; it works:

    // class t {   // Remove it
    
            public static void main(String args[]) {
    
                mythread m1 = new mythread("Child Thread 1");
                mythread m2 = new mythread("Child Thread 2");
    
                try {
    
                for(int i = 5 ; i > 0 ;i--) {
                    System.out.println("Main Thread" + i);
                    Thread.sleep(2000);
                }
    
            }
            catch(InterruptedException e){
    
                System.out.println("Main Thread Interrupted");
            }
    
    
        // }
    

提交回复
热议问题