割り当てを、GeoLocationタイプの囲みインスタンスで修飾する必要があります


109

このエラーが発生しています

タイプGeoLocationの囲んでいるインスタンスにアクセスできません。割り当てを、GeoLocationタイプの囲んでいるインスタンスで修飾する必要があります(egxnew 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
:この回答を参照してくださいstackoverflow.com/questions/633585/...
hmjd

回答:


150

こんにちは私はこれの解決策を見つけました;-)

このエラーはservice.submit(new ThreadTask(i)); 、メインクラスのインスタンスを作成せずに内部クラスのインスタンスを作成しようとしているために発生します。

この問題を解決するには、まずメインクラスのインスタンスを作成してください:

GeoLocation outer = new GeoLocation();

次に、呼び出す予定のクラスのインスタンスを次のように作成します。

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

これで問題が解決することを願っています;-)


101

別のオプション、そして私が好むものは、内部クラスを静的に設定することです。

public static class ThreadTask implements Runnable { ... }

私にとっても完璧に働いた。1)最も簡単であり、2)メインのメソッドが静的であるため、これは推奨されるソリューションであると思います。
アラン

15

インラインクラスを作成し 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);
    }
}

4
SOへようこそ。ここでは、ソリューションを使用する理由だけでなく、理由を説明することをお勧めします。それはあなたの答えをより価値のあるものにし、あなたがそれをどのように行うかについてのより良い理解を得るためにさらなる読者を助けるでしょう。また、FAQ:stackoverflow.com/faqをご覧になることをお勧めします。
ForceMagic

2

内部クラスのインスタンスを作成するには、親クラスのインスタンスを作成する必要があります。次に例を示します。

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

これは、静的メソッドまたは同様に非静的メンバーにアクセスしている場合にも発生する可能性があります。以下は2つの異なる側面です。1つはエラーを引き起こし、もう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はStackArrListのインスタンスであるxnew A()など)で割り当てを修飾する必要があります。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;

        }

    }

}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.