必须使用GeoLocation类型的封闭实例限定分配

时间:2012-03-16 21:17:29

标签: java

我收到此错误 -

无法访问GeoLocation类型的封闭实例。必须使用GeoLocation类型的封闭实例限定分配(egxnew A(),其中x是GeoLocation的实例)。此错误发生在 新的ThreadTask(i) 即可。我不知道为什么会这样。任何建议将不胜感激。

public class GeoLocation {
    public static void main(String[] args) throws InterruptedException {
        int size = 10;

        // create thread pool with given size
        ExecutorService service = Executors.newFixedThreadPool(size); 

        // queue some tasks
        for(int i = 0; i < 3 * size; i++) {
            service.submit(new ThreadTask(i));
        }

        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }

    class ThreadTask implements Runnable {
        private int id;

        public ThreadTask(int id) {
            this.id = id;
        }

        public void run() {
            System.out.println("I am task " + id);
        }
    }

}

6 个答案:

答案 0 :(得分:144)

您好我找到了解决方案;-)

发生此错误是因为您尝试创建内部类service.submit(new ThreadTask(i));的实例 没有创建主类的实例..

要解决此问题,请先创建主类的实例:

GeoLocation outer = new GeoLocation();

然后创建要调用的类的实例,如下所示:

service.submit(outer.new ThreadTask(i));

我希望这能解决你的问题; - )

答案 1 :(得分:97)

另一个选项,也就是我喜欢的选项,是将内部类设置为静态。

public static class ThreadTask implements Runnable { ... }

答案 2 :(得分:15)

制作内联类static

public class OuterClass {

    static class InnerClass {
    }

    public InnerClass instance = new OuterClass.InnerClass();
}

然后您可以按如下方式实例化内部类:

new OuterClass.InnerClass();

答案 3 :(得分:3)

执行此结构:

FILE GeoLocation.java

public class GeoLocation {

    public static void main(String[] args) throws InterruptedException {

        int size = 10;

        // create thread pool with given size
        ExecutorService service = Executors.newFixedThreadPool(size); 

        // queue some tasks
        for(int i = 0; i < 3 * size; i++) {
            service.submit(new ThreadTask(i));
        }

        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }

}

档案ThreadTask.java

public class ThreadTask implements Runnable {
    private int id;

    public ThreadTask(int id) {
        this.id = id;
    }

    public void run() {
        System.out.println("I am task " + id);
    }
}

答案 4 :(得分:1)

如果从静态方法或类似方法访问非静态成员,也可能发生这种情况。 以下是两个不同的方面,一个导致错误和其他解决的代码片段。 这只是让其他人成为班级&#34;静态&#34;

的问题
package Stack;

import java.util.Stack;
import java.util.*;

public class StackArrList {

    public static void main(String[] args) {


        Scanner in = new Scanner(System.in);

        Stack S = new Stack();
        System.out.println("Enter some integers and keep 0 at last:\n");
        int n = in.nextInt();

        while (n != 0) {
            S.push(n);
            n = in.nextInt();
        }
        System.out.println("Numbers in reverse order:\n");

        while (!S.empty()) {

            System.out.printf("%d", S.pop());
            System.out.println("\n");

        }

    }

    public class Stack {
        final static int MaxStack = 100;
        final static int Value = -999999;
        int top = -1;
        int[] ST = new int[MaxStack];

        public boolean empty() {
            return top == -1;
        }

        public int pop() {

            if (this.empty()) {
                return Value;
            }
            int hold = ST[top];
            top--;
            return hold;
        }

        public void push(int n) {
            if (top == MaxStack - 1) {
                System.out.println("\n Stack Overflow\n");
                System.exit(1);
            }
            top++;
            ST[top] = n;

        }

    }

}

这会抛出错误无法访问StackArrList类型的封闭实例。必须使用StackArrList类型的封闭实例限定分配(例如x.new A(),其中x是StackArrList的实例)。并且不允许生成Stack类的实例

当您将类Stack 设置为静态类Stack 时,它将正常工作,并且不会出现错误。

package Stack;

import java.util.Stack;
import java.util.*;

public class StackArrList {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Stack S = new Stack();
        System.out.println("Enter some integers and keep 0 at last:\n");
        int n = in.nextInt();

        while (n != 0) {
            S.push(n);
            n = in.nextInt();
        }
        System.out.println("Numbers in reverse order:\n");

        while (!S.empty()) {

            System.out.printf("%d", S.pop());
            System.out.println("\n");

        }

    }

    static class Stack {
        final static int MaxStack = 100;
        final static int Value = -999999;
        int top = -1;
        int[] ST = new int[MaxStack];

        public boolean empty() {
            return top == -1;
        }

        public int pop() {

            if (this.empty()) {
                return Value;
            }
            int hold = ST[top];
            top--;
            return hold;
        }

        public void push(int n) {
            if (top == MaxStack - 1) {
                System.out.println("\n Stack Overflow\n");
                System.exit(1);
            }
            top++;
            ST[top] = n;

        }

    }

}

答案 5 :(得分:1)

您需要创建父类的实例才能创建内部类的实例。这是一个例子:

url