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

我得到这个错误作为-

无法访问 GeoLocation 类型的封闭实例。必须使用类型为 GeoLocation 的封闭实例限定分配(例如,x.new A () ,其中 x 是 GeoLocation 的实例)。这个错误出现在 新的线程任务(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);
}
}


}
100394 次浏览

This error happens because you're trying to create an instance of an inner class service.submit(new ThreadTask(i)); without creating instance of main class..

To resolve this issue please create instance of main class first:

GeoLocation outer = new GeoLocation();

Then create instance of class you intended to call, as follows:

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

Another option, and the one I prefer, would be to set the inner class to be static.

public static class ThreadTask implements Runnable { ... }

Do this Structure:

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

}

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

Make the inline class static.

public class OuterClass {


static class InnerClass {
}


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

Then you can instantiate the inner class as follows:

new OuterClass.InnerClass();

This may happen too if you are accessing non-static members from the static methods or likewise. Following are two different aspects, one which cause the error and other solved piece of code. it's just the matter of making other as class "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;


}


}


}

This throws the error 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). and will not allow to make instance of Stack class

When you make the class Stack to static class Stack will work fine and no error will be there.

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;


}


}


}

You need to create an instance of the parent class in order to create instances of your inner classes. Here is an example:

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