Four internal classes and differences

Source: Internet
Author: User

The main functions of the inner class are as follows:

1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class

2. The method of the inner class can access all the data of the external class directly, including the private data

3. Functions implemented by internal classes can be implemented using external classes, but sometimes it is easier to use internal classes.

Java has four main internal classes: member inner class, Static inner class, method inner class, anonymous inner class .

A member inner class is similar to a normal member variable, defined at the property level, and can access any property of the outer class. When used, new external class. New Inner class.

Static inner classes do not allow access to non-static properties of external classes. Can be used as a direct new.

The inner class of a method is a local variable that defines a class within a method, similar to a method. Only used in this method. can be directly new.

The anonymous inner class is a special method inner class. It is mainly used to implement the interface or abstract class, to achieve the purpose of single use, and to create a class file less. The Use-time interface (abstract class) name Instance name =new Interface (abstract Class) name () {interface (abstract class) implementation}.

Four internal classes and differences

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.