Monday, December 9, 2013

[Java][Solved with image] No enclosing instance of type xxx is accessible.

My background story is using "class A" (ListXMLParser.java) to call function in "class B"(ListMode.java),
and then I move the code  from "class B"(ListXMLParser.java) to "class A"(ListMode.java) .
(It looks I change the declaring class to enclosing class) and get the error : "No enclosing instance of type xxx is accessible. "


Full Error msg :
No enclosing instance of type ListXMLParser is accessible. Must qualify the allocation with an enclosing instance of type ListXMLParser (e.g. x.new A() where x is an instance of ListXMLParser).

And i get a nice answer from internet,  that is from seh in stackoverflow.
he said need add the "static" for enclosing class. For my case:

Before
public class ListModel { ... }
After
public static class ListModel { ... }

Related image:


Original answer :

If you change class MyThread to be static, you eliminate the problem:
public static final class MyThread implements Runnable
Since your main() method is static, you can't rely on non-static types or fields of the enclosing class without first creating an instance of the enclosing class. Better, though, is to not even need such access, which is accomplished by making the class in question static.
share|improve this answer
add comment

Reference:
http://stackoverflow.com/questions/12471664/no-enclosing-instance-of-type-is-accessible

No comments :

Post a Comment