Embedded classes in Java

Source: Internet
Author: User

In Java, the embedded class (NestedClass) can be defined in a class in Java programming language. This "other" class is called an embedded class. Example: [java] class OuterClass {... Class NestedClass {...} Term: There are two types of embedding: static and Non-static. Static embedded classes are simply called static embedded classes ". Non-static embedded classes are called internal classes. [java] class OuterClass {... Static class StaticNestedClass {...} Class InnerClass {...} An embedded class is a member of its class. Non-static embedded classes (internal classes) can access other members of the classes that contain them, even if these Members may be private ). Static embedded classes cannot access other members of the classes that contain them. As a member of the class containing it, the embedded class can be private, public, and protected ), or package private ). (During the meeting, the classes outside the meeting can only be public or private ).) Why use an embedded class? There are several situations that need to be used to embed classes. Among them, this is a way to group those classes that are only used in one place. Embedded classes enhance code encapsulation. Embedded classes can improve code readability and maintainability. Logical grouping of classes -- if a class is useful in another class, embedding it in another class can ensure that they are logically related, you can also keep them together in code. Embedding This helper class into this class also makes the entire package more reasonable. Enhanced encapsulation-consider two top-level classes A and B. If B needs to access some Members in A, these Members must be declared as public ). However, only B can access these members. That is to say, apart from B, other classes are not allowed to access these members. Therefore, these members can be private only for B's reasons, we have to declare these members as public. In this case, if you use an internal class to embed Class B into Class A, these members can be declared as private and Class B can also access these members. In addition, Class B itself is hidden from the outside world. Enhance code readability and maintainability-embed small classes into top-level classes so that the code of these classes is put together with the code that uses them. Static embedding methods, methods, and variables are similar to classes. A static embedding class is related to the "external" class containing it. Like static class methods, static embedded classes cannot directly contain instance variables and methods of their classes-they can only reference these variables and methods through objects. Note: static dive classes can interact with external classes and other classes like other top-level classes. In fact, static classes act as top-level classes embedded in other classes, so as to facilitate packaging. To use a static embedded class, you must use its "external" Class Name: OuterClass. staticNestedClass. For example, to create a static embedded class object, you can use the following statement: OuterClass. staticNestedClassnestedObject = new OuterClass. staticNestedClass (); internal classes and methods and variables are the same as those related to the instance of the class, and internal classes are also related to instances of the "external" class, in addition, you can directly use the methods and fields of this object. Because internal classes are related to an object, they cannot define static members themselves. Internal class objects exist in the "external" class objects of the internal class. Suppose there are the following classes: [java] class OuterClass {... Class InnerClass {...} An internal class instance can only exist in its "external" class instance, and can access the instance fields and methods of its external class. The figure below shows this: Objects of internal classes exist in objects of external classes. If you want to instantiate the internal class, you must first instantiate the external class. Then, use the following statement to create an internal Class Object: OuterClass. innerClassinnerObject = outerObject. new InnerClass (); in addition, there are two special internal classes: Local class and Anonymous class (anonymousclass, sometimes called anonymous internal class ). We will discuss these two special internal classes in the next section. Note: If you want to know

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.