public class Close {
public static void main(String[] args){
try{
Resource res = new Resource();//This compilation prompts an error
res.dosome();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class Resource implements AutoCloseable{
void dosome(){
System.out.println("Do something");
}
public void close() throws Exception{
System.out.println("Resources are closed");
}
}
}
Error content: no enclosing instance of type close is accessible. Must qualify the allocation with an enclosing instance of type close (e.g. X.New a() where x is an instance of close)
that is, if there is no accessible instance of inner class E, an appropriate instance of inner class E must be assigned (such as X.New a (), X must be an instance of E.) looking at this prompt, I wonder why I have instantiated this class with new
It turns out that the internal class I wrote is dynamic, that is, it starts with public class. The main program is public static class main. In Java, static methods in a class cannot directly call dynamic methods. Only if an internal class is modified to a static class, then the member variable and member method of the class can be invoked in a static class. Therefore, without making other changes, the simplest solution is to change the public class to public static class.
Similar Posts:
- [Problems with the use of internal classes] No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclo
- 【Java】No enclosing instance of type XXX is accessible. Must qualify the allocation with an enclos…
- [Two Solutions] non-static method xxx() cannot be referenced from a static context
- Solution to the problem of Java program “resource leak: ‘SC’ is never closed” in vscode
- [Solved] Java Call Error: java.lang.IllegalArgumentException: wrong number of arguments
- [Solved] Java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
- Eclipse Error:The selection cannot be launched, and there are no recent launches
- Java Thread wait, notify and notifyAll Example
- [Solved] JAVA:java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcodbcDrive
- How to Solve Error: Scanner class java. Util. NoSuchElementException exception