必须使用GeoLocationtypes的封闭实例限定分配

我得到这个错误as-

没有可以访问GeoLocationtypes的封闭实例。 必须用GeoLocationtypes的封闭实例限定分配(egxnew A(),其中x是GeoLocation的一个实例)。 这个错误是在新的ThreadTask(i) 。 我不知道为什么会发生。 任何build议将不胜感激。

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

嗨,我find了这个解决scheme;-)

发生这个错误是因为你正在尝试创build一个内部类的实例service.submit(new ThreadTask(i)); 无需创build主类的实例

要解决这个问题,请先创build主类的实例:

 GeoLocation outer = new GeoLocation(); 

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

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

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

另一个select,我喜欢的一个select是将内部类设置为静态。

 public static class ThreadTask implements Runnable { ... } 

使内联类static

 public class OuterClass { static class InnerClass { } public InnerClass instance = new OuterClass.InnerClass(); } 

然后你可以实例化内部类如下:

 new OuterClass.InnerClass(); 

做这个结构:

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

如果您正在从静态方法或类似的方式访问非静态成员,也可能发生这种情况。 以下是两个不同的方面,一个是导致错误和其他解决的代码块。 这只是让其他人成为“静态”

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

这会抛出错误没有封闭typesStackArrList的实例是可访问的。 必须使用typesStackArrList的封闭实例(egxnew A(),其中x是StackArrList的实例)限定分配。 并不会允许创buildStack类的实例

当你把这个类堆栈静态类堆栈将正常工作,没有错误将在那里。

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

您需要创build父类的实例以创build内部类的实例。 这里是一个例子:

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