内部类中的非法静态声明

时间:2013-07-23 13:04:11

标签: java multithreading

import java.lang.*;

class mythread implements Runnable {

    Thread t1;
    String name = "";

    mythread(String thname){

        name = thname;
        t1 = new Thread(this, name);
        System.out.println("Child thread starting" + t1 );
    }

    @Override
    public void run() { 
        for(int i = 5 ; i > 0 ;i--){
            System.out.println("Name Of Thread" + t1 + i);
        }   
    }


    class t {

        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");
        }


     }
}

public static line中的错误:

Illegical static declaration in inner class `mythread.t` modifier static is allowed in constant variable declaration

3 个答案:

答案 0 :(得分:3)

根据错误消息,内部类无法访问静态变量;

删除类t或将其声明为静态;它有效:

// 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");
        }


    // }

答案 1 :(得分:1)

有一些缺少括号

import java.lang.*;

class mythread implements Runnable {

    Thread t1;
    String name = "";

    mythread(String thname){

        name = thname;
        t1 = new Thread(this, name);
        System.out.println("Child thread starting" + t1 );
    }

    @Override
    public void run() { 
        for(int i = 5 ; i > 0 ;i--){
            System.out.println("Name Of Thread" + t1 + i);
        }   
    }
} // was missing (for closing class mythread)


class t {

    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");
        }

     }
}

答案 2 :(得分:0)

我们不能在嵌套的内部类中使用静态方法,因为内部类与其外部类的对象隐式关联,因此它不能为自己定义任何静态方法。 所以,在这里你也删除了类 t //类t{

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");
    }

 }