Nested classes, internal classes, static internal classes in Java, and java nesting

Source: Internet
Author: User

Nested classes, internal classes, static internal classes in Java, and java nesting
In Java, we define another class within a class, as shown below:

class OuterClass {    ...    class NestedClass {        ...    }}

In the preceding example, OuterClass is called the external class (enclosing Class), and the class in it is called the Nested Class ).

There are two types of nesting classes: static and non-static, namely static and non-static nesting classes. A non-static nested Class is also called an Inner Class ). The Static internal Class we usually call is actually not strict. Strictly speaking, it should be called Static Nested Class ).

class OuterClass {    ...    class InnerClass {        ...    }    static class StaticNestedClass {        ...    }    }

In the above Code, InnerClass is an internal class, And StaticNestedClass is a static nested class.

Although both internal and static nesting classes are nested classes, there are some differences in usage.


Internal class

For example, the following internal classes are defined,

class OuterClass {    ...    class InnerClass {        ...    }}
OuterClass is the peripheral class of InnerClass, and InnerClass is the internal class of OuterClass. Instance objects of internal classes are bound to an instance object of peripheral classes, and InnerClass can access all member attributes and methods of the bound OuterClass, including private member attributes and methods. In InnerClass, OuterClass. this explicitly references the bound OuterClass instance. To instantiate an InnerClass internal class, you must first instantiate its peripheral class OuterClass, and then use the following syntax to create an internal class instance:

OuterClass outerObject = new OuterClass();OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Note: The above is written as outerObject. new InnerClass () instead of new OuterClass. innerClass (); we are executing the code OuterClass. innerClass innerObject = outerObject. when new InnerClass () is used, two things are actually done. One is to create an innerObject instance of an internal class, and the other is to bind innerObject to an outerObject instance of its peripheral class. In this way, the innerObject can access all the member attributes and methods in the outerObject. What if I want to directly skip the peripheral class to initialize the internal class? The Code is as follows:
If you execute the code InnerClass innerObject = new InnerClass (), the following compilation error occurs: No enclosing instance of type OuterClass is accessible. must qualify the allocation with an enclosing instance of type OuterClass (e.g. x. new A () where x is an instance of OuterClass ). the error message provided by the compiler is clear, that is, We initialize the internal class without the instance of the peripheral class OuterClass. It must be written in the form of x. new InnerClass (), where x is an instance object of the peripheral class OuterClass.
Static nesting class
Some people regard static Nested classes as static internal classes. In fact, static internal classes are not strictly called because internal classes are non-static. Static nesting classes are very different from internal classes. Static nesting classes are static classes, but they are located inside a class. Suppose there is a definition of the following static nested class:
class OuterClass {    ...    static class StaticNestedClass {        ...    }}
So I can use a static nested class like a normal static class, but I only need to access the name of the static nested class through the name of its peripheral class. Therefore, the peripheral class is more like the namespace of the static nested class. For example, to obtain a static nested class, you must use OuterClass. StaticNestedClass. To create a strength object for a static nested class, use the following syntax:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Because static Nested classes are essentially static classes, initialization of their instance objects does not need or need to be bound to a peripheral class object like an internal class. Because the static nested class does not bind external class objects as the internal class, there is no such statement that the static nested class cannot access the members of its peripheral class. What if we initialize a static nested class like initializing an internal class, that is, to bind it to the instance object of its peripheral class when creating a static nested class? The Code is as follows:

We can find that if we forcibly bind the instance Object of the peripheral class to the static nested class during initialization, the compiler will report the following error: Illegal enclosing instance specification for type OuterClass. staticNestedClass we provide the static nested class OuterClass. staticNestedClass specifies an invalid peripheral class instance.

To sum up, although both the internal class and the static nested class belong to the nested class, there is an essential difference between the two: the instantiation object of the internal class needs to be bound to an instantiation object of the peripheral class, however, the instantiated objects of static Nested classes cannot be bound to the instantiated objects of peripheral 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.