内部类中的非法静态声明 [英] Illegical static declaration in inner class

查看:900
本文介绍了内部类中的非法静态声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


     }
}

错误是在公共静态行

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


推荐答案

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

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

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

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


    // }

这篇关于内部类中的非法静态声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆