Help Us Improve. Please let us know if you were asked any question in your interview.

More Questions

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

Error

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

Error Type

Compilation


Cause

Trying to initialize the instance of inner class without specifying the instance of outer class. Inner classes are not accessible directly using the Outer class name as only static inner classes are accessible that way.


Sample Code

public class OuterClass {

     public class InnerClass {
     }


OuterClass.InnerClass = new OuterClass.InnerClass(); // this line gives error

Resolution

Either make the inner class as static inner class

public class OuterClass {

     public static class InnerClass {
     }


OuterClass.InnerClass = new OuterClass.InnerClass(); 

But with this, the copy of the inner class will not be object specific but class specific


Or 

Initialize the Inner class using the Outer class instance

public class OuterClass {

     public class InnerClass {
     }


OuterClass.InnerClass = new OuterClass.new InnerClass(); 




Stay updated with Java posts in your email.

Enter your email address:


Delivered by FeedBurner
Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus




×

Would like to try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests. It's updated daily and It's Awesome.


×

Would like to try our new Java Practice Test tool.

There are more than 100 questions currently and expanding quickly. It's updated daily and It's Awesome.