No enclosing instance of type XXX is accessible problem in Java

Source: Internet
Author: User

Java has encountered a problem in writing code, when an instance of an inner class was created in the main method, an error occurred during the compile phase to see the error description:

Multiple markers at the
-The value of the local variable test is not used
-No enclosing instance of type staticcalldynamic is accessible. Must qualify the allocation with an enclosing instance of type staticcalldynamic (e.g.
X.new A () where x is an instance of staticcalldynamic).

It is verified that because the inner class is dynamic (no static keyword decoration), and the main method is static, the ordinary inner class object implicitly holds a reference to the outer class object that created it, so you must first create an outer class when the static method (the class is already initialized) to invoke the inner class. That is, you should create "danymictest test = new Staticcalldynamic ()." New Danymictest (); Where Staticcalldynamic is an external class, danymictest is an internal dynamic class, and if you modify the inside as a static class, you can create an inner class instance directly in Main.

By the way, the static method calls the dynamic method, if the static method is called directly in the static method, where print () is the dynamic method in the class, the following problem occurs:

Cannot make a static reference to the Non-static method print () from the type staticcalldynamic

This is to be analyzed from the memory mechanism of Java, first, when you new an object, not first in the heap to open up memory space for the object, but first the static method in the class (static function with static modifier) loaded into a place called the method area, and then create the object in the heap memory. Therefore, static methods are loaded as the class loads. When you new an object that exists in memory, the This keyword generally refers to the object, but it is also possible to invoke the static method of the class with the class name without the new object.

The program is ultimately executed in memory, the variables are accessed only in memory, the static members of the class (Metamorphosis and methods) belong to the class itself, when the class is loaded with memory, can be directly accessed through the class name, non-static members (variables and methods) belong to the object of the class, Therefore, the memory is allocated only when the object of the class is generated (creating the instance) and then accessed through the object of the class. Accessing a non-static member in a static member of a class can be an error because a static member already exists when the non-static member of the class does not exist, and accessing something that does not exist in memory is of course an error.

So either the method is changed to a static method, or it is invoked by the instance by creating the instance where the method resides.

Reference: http://www.2cto.com/kf/201502/375549.html

No enclosing instance of type XXX is accessible problem in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.