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

111

我遇到了这个错误:

没有外围类GeoLocation的实例可访问。 必须使用GeoLocation类型的外围实例来限定分配(例如,x.new A()其中x是GeoLocation的实例)。 这个错误发生在new 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);
        }
    }

}

2
请参考这个答案:https://dev59.com/3XRB5IYBdhLWcg3wZmhz - hmjd
可能是重复的问题:Java - No enclosing instance of type Foo is accessible - Raedwald
6个回答

155

这个错误发生是因为您试图创建一个内部类的实例service.submit(new ThreadTask(i));,但没有先创建主类的实例。

为了解决这个问题,请先创建主类的实例:

GeoLocation outer = new GeoLocation();

然后按照以下方式创建您打算调用的类的实例:

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

102

另一种选择,而且我更喜欢的是将内部类设置为静态。

public static class ThreadTask implements Runnable { ... }

15

将内联类(class)设置为static

public class OuterClass {

    static class InnerClass {
    }

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

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

new OuterClass.InnerClass();

3

请按照以下结构完成:

文件 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);
    }
}

3

为了创建内部类的实例,您需要先创建父类的实例。以下是一个示例:

package RandomTests;

public class FinalConstructorTest {


    public static void main (String [] arg){
        FinalConstructorTest fct= new FinalConstructorTest();
        InnerClass1 f1= fct.new InnerClass1(99);
        InnerClass2 f2= fct.new InnerClass2();
    }

    class InnerClass1{
        private final int num2;

        protected InnerClass1(int num){
            num2= num;
            System.out.println("num2= "+ num2);
        }
    }
    class InnerClass2{
        //private static final int x; //Doesn't work
        private final int y; 

        {
            y= 5;
            System.out.println("y= "+ y);
        }
    }
}

1
如果您从静态方法中访问非静态成员或类似情况,也可能会发生这种情况。以下是两个不同的方面,一个是导致错误的原因,另一个是解决代码。 只需要将另一个类设置为"static"即可解决问题。
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;

        }

    }

}

这会引发错误 No enclosing instance of type StackArrList is accessible. Must qualify the allocation with an enclosing instance of type StackArrList (e.g. x.new A() where x is an instance of StackArrList).,并且不允许创建 Stack 类的实例。
当你将 class Stack 改为 static class 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;

        }

    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接