简体   繁体   中英

Illegical static declaration in inner class

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


     }
}

THE ERROR IS GIVEN IN public static line :

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;

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


    // }

there are some missing brackets

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

     }
}

we can't have static method in a nested inner class because an inner class is implicitly associated with an object of its outer class so it cannot define any static method for itself. So ,here you have too remove class t //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");
    }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM