The difference between Static Nested class and Inner class

Source: Internet
Author: User

The difference between Static Nested class and Inner class

An inner class is a class defined inside a class that cannot define a static member (static member is not an object's property, just to find a shelter, so it needs to be put into a class, so small, you have to put it into a class inside the class, too!)! Provide inner class, not for letting you do this kind of thing, boring, not letting you do it. I think it might be that since static members are like global variables of the C language, and inner classes are often used to create internal objects, it is meaningless to put "global variables" in internal classes, and since they are meaningless things, they should be banned), and the inner classes can directly access member variables in the outer class. An inner class can be defined outside the method of an external class, or it can be defined in the method body of an external class, as follows:


The access type of an inner class defined outside the method body can be a public,protecte, default, private, etc. 4 types, which is like the member variables defined in the class have 4 access types, they determine whether the definition of this inner class is visible to other classes, and in this case, We can also create an instance object of an inner class outside, and when creating an instance object of an inner class, you must first create an instance object of the outer class, and then use the instance object of the outer class to create an instance object of the inner class, with the following code:


An inner class defined inside a method cannot have an access type modifier in front of it, just as a local variable is defined in a method, but a final or abstract modifier can be used before this inner class. This inner class cannot be referenced by other classes that are not visible to other classes, but the instance objects created by this inner class can be passed to other classes for access. This inner class must be defined first, and then used, that is, the definition code of the inner class must appear before the class is used, which is the same as if the local variable in the method must be defined before it is used. This inner class can access local variables in the method body, but must have a final modifier before the local variable.


For these details, as long as you try to write code in Eclipse, you can get a quick look at the various types of error messages that are developed by the ToolTip.


Inside the method body, you can also create an anonymous inner class that defines a subclass of an interface or class, and also creates an instance object of the subclass, without defining a name for the subclass:


Finally, the inner class defined outside the method can be preceded by the static keyword, which becomes the static Nested class, which no longer has an intrinsic class, and all, narrowly speaking, is not an inner class. There is no difference between Static Nested class and normal class at run time, but there are some differences in syntax when programming reference, it can be defined as public, protected, default, private, etc. The normal class can only be defined as public and the default two types. Outside the name of the static Nested class class is "external class name. Internal class name". You can create the static Nested class directly outside without creating an instance object of the external class, for example, assuming that inner is a static Nested class defined in the outer class, you can create a inner class using the following statement:


Because the static Nested class does not depend on the instance object of the external class, the static Nested class can access the non-static member variables of the outer class. When you access the static Nested class in the external class, you can directly use the name of the static Nested class without adding the name of the outer class, and in the static Nested class you can refer directly to the static member variables of the outer classes. You do not need to add the name of an external class.


The inner class defined in the static method is also static Nested class, at this time cannot add the static keyword in front of the class, static Nested class is similar to how the inner class in the normal method is applied In addition to the direct access to static member variables in the outer class, it is possible to access local variables in the statically method, but the local variable must be added to the final modifier before it.


Note: First of all, according to your impression of the overall aspects of the internal class characteristics: for example, in two places can be defined, can access the external class member variables, cannot define static members, which is a big feature. And then we'll talk about some details, such as the syntax differences of several definitions, the static inner classes, and the anonymous inner classes.


The difference between Static Nested class and Inner class

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.