Java Internal classes

Source: Internet
Author: User

An inner class (Inner class) is a class that is defined in another class, a compiler phenomenon that is independent of the virtual machine. Benefits of the Inner class:     1. Hide mechanism: Inner classes can be hidden from other classes in the same package     2. Inner classes Learn about the outer classes: The inner class method can access the data in the scope where the class definition resides, including private data four inner classes: member inner class Member inner class static inner class static inner class  ( Nested class) Local inner class Local inner class anonymous inner class anonymous inner  Class------------details--------------------1. member Inner class: All member variables and methods of the external class that can access it (regardless of static, non-static) themselves do not have static members inside the outer class to create an instance of the member inner class:     this.new innerclass (); Creates an instance of an inner class outside the outer class:     new outerclass (). New  innerclass (); Accesses the members of the external class in the inner class:     outerclass.this.member2. Static inner Class (in Java interview, if you say "nested Class" is also it, Estimated that the subject is C + + turned) (no practical use) the inner class is used only to hide a class inside another class, does not require an inner class to refer to a perimeter object, can declare an inner class as static, so that the resulting reference   can access only static members and static methods of the outer class. Includes private static members and methods to generate static inner class objects in the following way: Outerclass.innerclass inner = new Outerclass.innerclass (); 3. Local inner classes are defined, used in methods, cannot be modified by public, protected, private and static (like local variables); only local variables of the final type defined in the method can be accessed (cannot be changed after initialization) ; defined in a method, so it can only be used in methods. 4. Anonymous InternalClass (with most) a local inner class that has no name, is used to create only one object of the class (so it does not have to be named), usually as a parameter to a method, implicitly inherits a parent class, or implements an interface; restriction: Because the constructor name is the same as the class name, and the anonymous class does not have a class name, Anonymous classes cannot have a construction method;                                             ----------Comparison----------------------------
Their own Peripheral class
Static members Non-static Members Static members Non-static Members
member Inner class No Xxx Xxx Xxx/outclass.this.xxx
Static Inner class Xxx Xxx Xxx/outclass.xxx Cannot be used
Local inner class No Xxx Xxx Xxx/outclass.this.xxx
Anonymous inner class No Xxx Xxx Xxx/outclass.this.xxx
It can be seen that the static inner class comes in one row, and the other three types of contracts. A local inner class is like a smaller version of a member's inner class, and an anonymous inner class is simply a simplified version of a local inner class; No wonder there's a self-made Java panel question that says "compare static nested classes with internal classes"

Java Internal classes

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.