The combination of the outer and inner classes + static and non-static variables of Java

Source: Internet
Author: User

See Li Gang "crazy Java Handout", inside the interior of the class feel a bit scattered and not all, after reading or is not very clear how to use, so I wrote a program test a bit. You can see the rules of mutual invocation between an outer class and an inner class + static and non-static members, as shown in the following code.

The results of the operation are as follows:

Summarized as follows:

Note: When the error follows: "cannot refer to non-static from a static context", the problem is that a non-static method or a class that belongs to a non-static variable may not be actually serialized. Because you want to use a non-static method, you must instantiate the class to which it belongs.

For example, change the 40th line of code to INTER2. Staticinter ();

Compile:

The reason is that Staticinter (), although it is a static inner class, is itself a non-static method (instance method) and cannot be called by the inner class class name. Non-static method to invoke the method.

The correct approach is to instantiate the class to which the non-static method belongs, whether the owning class is a static class or a non-static inner class, that is, create an object of the owning class, and then call the non-static method.

The combination of the outer and inner classes + static and non-static variables of 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.