Must qualify the allocation with an enclosing instance of type GeoLocation

Java

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

public static class ThreadTask implements Runnable { ... }

Solution 3 - Java

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

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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;

		}

	}

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarsenalView Question on Stackoverflow
Solution 1 - Javauser1528582View Answer on Stackoverflow
Solution 2 - JavaDanOView Answer on Stackoverflow
Solution 3 - JavakangearView Answer on Stackoverflow
Solution 4 - JavaRafael PereiraView Answer on Stackoverflow
Solution 5 - JavaGeneView Answer on Stackoverflow
Solution 6 - JavaGaurav KariaView Answer on Stackoverflow