The inner class of Java

Source: Internet
Author: User

Java's inner class (Inner Class) is similar to the nested class (Nested Class) that C + + says, except that it is a different story.

Java has 4 forms of inner class: Static inner class, member inner class (Ordinary inner Class), local inner class, anonymous inner class.

For example:

classouterclass{Static classinnerclass{}//static inner class}classouterclass{classinnerclass{}//member Inner Class (Ordinary inner Class)}classouterclass{ Public voidmemberfunction () {classinnerclass{}//local inner class    }}classMyFrameextendsframe{//External Class     PublicMyFrame () {Addwindowlistener (NewWindowadapter () {//Anonymous Inner class             Public voidwindowclosing (WindowEvent e) {Dispose ();            System.exit ();    }        }); }}
    • The characteristic of a static inner class: An inner class that is declared static, which can be instantiated without relying on an instantiation of an external class , while an ordinary inner class needs to be instantiated after an external class is instanced. A static class cannot have the same name as an external class, cannot access ordinary member variables of an external class, can access only static members of an external class, Static methods (including private types), constructors.
    • member Inner class: The static inner class removes the static keyword and becomes the normal member inner class. The member inner class is a non-static inner class that accesses all the properties and methods of the external class. However, it is bound to an instance of an external class, so there can be no static properties and methods . A member inner class can only be instantiated after an external class is instantiated .
    • Local inner class: refers to a class defined within a block of code that is scoped to its code block and is the least used type in an inner class. Local inner classes, like local variables, cannot be modified by public, protected, private, static, only local variables defined in the method as final types. A local static inner class is the same as the basic property of a static inner class.
    • Anonymous inner class: is an inner class that has no class name, does not apply to class,extends,implements, has no constructors, and it must inherit other classes or implement other interfaces . The benefit of using anonymous inner classes is to make your code more concise, but it can make your code less readable. It is generally used for event handling in GUI programming. Anonymous inner Classes cannot define their own static members, methods, and classes . The anonymous inner class must be followed by new, the anonymous inner class is also a local inner class, and all requirements for the local inner class apply to the anonymous inner class.

Note: The above content is excerpted from he Haoyuan's "Java Programmer interview written book".

The inner class of Java

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.