No enclosing instance of type is accessible

The java No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type (e.g. x.new A() where x is an instance of ). exception happens when an instance is created for an inner class in the outer class static method. The static method attempts to create an object of an inner class that belongs to an outer class instance.

The non static member variables cannot be accessed by a class static method. The instance of a non static inner class cannot be created by a static class method. Both the method and the inner class should be modified as static or both should not be static. Otherwise, the exception No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type (e.g. x.new A() where x is an instance of ). will be thrown.

The inner class scope will be the same as the class member variable. The inner class can be declared as a static class as a static member variable. The instance of a non static inner class will be created with in the outer class object. The instance of the inner static class would be accessible at the class level. It can then be created in both static and not static methods.



Exception

The java error message will be seen as below in the eclipse.

No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).

The stack trace of the exception will be shown as shown below.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	No enclosing instance of type SubListTest is accessible. Must qualify the allocation with an enclosing instance of type SubListTest (e.g. x.new A() where x is an instance of SubListTest).

	at com.yawintutor.SubListTest.load(SubListTest.java:10)
	at com.yawintutor.SubListTest.main(SubListTest.java:6)


Root Cause

Static methods can have access to either static variables or static methods. Static methods could not have access to non static variables or non static methods. Static methods are not allowed to have access to a non static inner class. An exception will be thrown if a static method attempts to create an instance of a non static inner class.



How to reproduce this exception

When the static method tries to create an instance of a non static inner class, the static method creates an exception. The static method would not be allowed to access the inner class that belongs to the outer class object. The example below reproduces the exception No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type (e.g. x.new A() where x is an instance of ).

package com.yawintutor;

public class SubListTest {
	
	public static void main(String[] args) {
		load();
	}
	
	public static void load() {
		Department dept = new Department();
		System.out.println(dept.getName());
	}
	
	public class Department {
		public String name="dept";

		public String getName() {
			return name;
		}
	}
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	No enclosing instance of type SubListTest is accessible. Must qualify the allocation with an enclosing instance of type SubListTest (e.g. x.new A() where x is an instance of SubListTest).

	at com.yawintutor.SubListTest.load(SubListTest.java:10)
	at com.yawintutor.SubListTest.main(SubListTest.java:6)


Solution 1

If the non static inner class is modified to the static inner class, the static method will provide access to the static inner class. Changing the non static to the static inner class would fix the exception.

package com.yawintutor;

public class SubListTest {
	
	public static void main(String[] args) {
		load();
	}
	
	public static void load() {
		Department dept = new Department();
		System.out.println(dept.getName());
	}
	
	public static class Department {
		public String name="dept";

		public String getName() {
			return name;
		}
	}
}

Output

dept


Solution 2

If the static method is modified to a non static method, both the method and the inner class would be within the scope of the object. The non static method can be used to access the non static inner class. In this case, the instance of the outer class will be used.

package com.yawintutor;

public class SubListTest {
	
	public static void main(String[] args) {
		SubListTest subListTest = new SubListTest();
		subListTest.load();
	}
	
	public void load() {
		Department dept = new Department();
		System.out.println(dept.getName());
	}
	
	public class Department {
		public String name="dept";

		public String getName() {
			return name;
		}
	}
}

Output

dept


Solution 3

If the inner class is created outside the outer class or in another file, the inner class would be created as a separate class in the package. The class is going to be created like any other class. The exception will not be thrown.

SubListTest.java

package com.yawintutor;

public class SubListTest {
	
	public static void main(String[] args) {
		load();
	}
	
	public static void load() {
		Department dept = new Department();
		System.out.println(dept.getName());
	}
}

Department.java

package com.yawintutor;

public class Department {
	public String name="dept";

	public String getName() {
		return name;
	}
}

Output

dept


Solution 4

When an instance of a class is created once in a static method, an instance of an outer class is created in a static method. The instance of the inner class can be created using the object of the outer class.

package com.yawintutor;

public class SubListTest {
	
	public static void main(String[] args) {
		load();
	}
	
	public static void load() {
		SubListTest subListTest = new SubListTest();
		Department dept = subListTest.new Department();
		System.out.println(dept.getName());
	}
	
	public class Department {
		public String name="dept";

		public String getName() {
			return name;
		}
	}
}

Output

dept


Solution 5

When an instance of a class is created once in a static method, an instance of an outer class is created in a static method. The instance of the inner class can be created using the object of the outer class. This is an another way of creating instance of the inner class using the local variable.

package com.yawintutor;

public class SubListTest {
	
	public static void main(String[] args) {
		load();
	}
	
	public static void load() {
		Department dept = new SubListTest().new Department();
		System.out.println(dept.getName());
	}
	
	public class Department {
		public String name="dept";

		public String getName() {
			return name;
		}
	}
}

Output

dept



Leave a Reply