必须与类型GeoLocation中

问题描述:

我收到此错误的类实例限定分配原样必须与类型GeoLocation中

类型的地理定位的无封闭情况下进行访问。必须使用GeoLocation类型的封闭实例来限定分配(例如x.new 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); 
     } 
    } 

} 
+2

看到这个答案:http://*.com/questions/633585/strange-syntax-for-instantiating-an-inner-class [爪哇 – hmjd 2012-03-16 21:27:03

+0

可能的复制 - 无外围实例的类型Foo是可以访问](http://*.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – Raedwald 2016-03-02 22:40:39

嗨,我发现这个;-)

这个错误是因为你想不创建主类的实例来创建一个内部类service.submit(new ThreadTask(i)); 实例的解决方案..

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

GeoLocation outer = new GeoLocation(); 

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

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

我希望这将解决您的问题;-)

另一种选择,和一个我喜欢,是设置内部类是静态的。

public static class ThreadTask implements Runnable { ... } 
+0

完美的工作很好谢谢。 – NagarjunaReddy 2014-02-17 13:16:02

做这个结构:

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

欢迎来到SO,在这里,解释为什么要使用您的解决方案而不仅仅是一个好的做法。这会让你的答案更有价值,并有助于读者更好地理解你是如何做到的。我还建议你看看我们的FAQ:http://*.com/faq。 – ForceMagic 2012-11-10 07:30:27

可以将嵌入类static

public class OuterClass { 

    static class InnerClass { 
    } 

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

然后你就可以实例化内部类,如下所示:

new OuterClass.InnerClass(); 

,如果你是从静态方法或访问同样非静态成员可能出现这种情况了。 以下是两个不同的方面,一个是导致错误和其他解决的代码段。 它使其他的类“静态”

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类静态类堆栈将正常工作,没有错误将在那里。

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; 

     } 

    } 

} 

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

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