2012-03-16 116 views
97

我收到此錯誤的類實例限定分配原樣必須與類型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://stackoverflow.com/questions/633585/strange-syntax-for-instantiating-an-inner-class [爪哇 – hmjd 2012-03-16 21:27:03

+0

可能的複製 - 無外圍實例的類型Foo是可以訪問](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – Raedwald 2016-03-02 22:40:39

回答

136

嗨,我發現這個;-)

這個錯誤是因爲你想不創建主類的實例來創建一個內部類service.submit(new ThreadTask(i)); 實例的解決方案..

要解決此問題,請首先創建主類的實例:

GeoLocation outer = new GeoLocation(); 

然後創建類的實例您打算調用,如下所示:

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

我希望這將解決您的問題;-)

92

另一種選擇,和一個我喜歡,是設置內部類是靜態的。

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

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

2

做這個結構:

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://stackoverflow.com/faq。 – ForceMagic 2012-11-10 07:30:27

13

可以將嵌入類static

public class OuterClass { 

    static class InnerClass { 
    } 

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

然後你就可以實例化內部類,如下所示:

new OuterClass.InnerClass(); 
1

,如果你是從靜態方法或訪問同樣非靜態成員可能出現這種情況了。 以下是兩個不同的方面,一個是導致錯誤和其他解決的代碼段。 它使其他的類「靜態」

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; 

     } 

    } 

} 
1

您需要創建父類的實例以創建內部類的實例。這裏有一個例子:

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