Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must qualify the allocation with an enclosing instance of type GeoLocation

Tags:

java

I am getting this error as-

No enclosing instance of type GeoLocation is accessible. Must qualify the allocation with an enclosing instance of type GeoLocation (e.g. x.new A() where x is an instance of GeoLocation). This error is coming on new ThreadTask(i). I don't know why is it happening. Any suggestions will be appreciated.

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

}
like image 317
arsenal Avatar asked Mar 16 '12 21:03

arsenal


4 Answers

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));
like image 200
user1528582 Avatar answered Sep 19 '22 11:09

user1528582


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

public static class ThreadTask implements Runnable { ... }
like image 26
DanO Avatar answered Sep 19 '22 11:09

DanO


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();
like image 32
kangear Avatar answered Sep 22 '22 11:09

kangear


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);
    }
}
like image 28
Rafael Pereira Avatar answered Sep 19 '22 11:09

Rafael Pereira