The following error occurred today while compiling a Java program.
No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).
The source code I originally wrote looked like this
public class Main
{
class Dog
{
private String name;
private int weight;
public Dog(String name, int weight)
{
this.setName(name);
this.weight = weight;
}
public int getWeight()
{
return weight;
}
public void setWeight(int weight)
{this.weight = weight;}
public void setName(String name)
{this.name = name;}
public String getName()
{return name;}
}
public static void main(String[] args)
{
Dog d1 = new Dog("dog1",1);
}
}
When this error occurred, I didn’t quite understand it.
After learning from other people’s explanation, I suddenly realized.
In the code, my dog class is an internal class defined in main. The dog inner class is dynamic, while my main method is static.
Just as static methods cannot call dynamic methods.
There are two solutions:
Method 1
Define the inner class dog as a static class.
Method 2
Define the inner class dog outside the main class.
Read More:
- Problems and causes of Java’s main function format (public static void main (string args()))
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- Java error: unable to find or load main class (package name in source file)
- Explicit and implicit conversion of Java data type
- Mongodb java version 3.X, prompt “XXX (an encryption algorithm) is not available” when there is a user name and password
- [Solved] No validator could be found for constraint ‘javax.validation.constraints.NotBlank’ validating type ‘java.lang.String’
- [Solved] JSON parse error: Cannot deserialize instance of `java.util.ArrayList<..> out of START_OBJECT token;
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- springboot jsp: There was an unexpected error (type=Not Found, status=404). No message available
- JAVA Error Illegal access: this web application instance has been stopped already. Could not load net.sf
- Byte 1 of UTF-8 sequence of error creating document instance. And bytes is invalid
- Java Running Error: Could not find or load main class
- [Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`
- [Solved] Docker Elasticsearch8.4.0 Error: Exception in thread “main” java.nio.file.FileSystemException
- [Solved] Scala error: type mismatch; found : java.util.List[?0] required: java.util.List[B]
- [Solved] org.apache.flink.client.program.ProgramInvocationException: The main method caused an error
- [Solved] Error installing MVN: Java_Home is set to an invalid directory
- [Solved] jar Run Error: no main manifest attribute
- [Solved] JAVA connect HBase program is stuck and does not report an error
- [Solved] Error resolving template [x] template might not exist or might not be accessible by any of the con