Differences between static nested class and inner class

Source: Internet
Author: User
An internal class is a class defined inside a class. A static member cannot be defined in an internal class. (a static member is not a feature of an object. It is just to find a reference, so you need to put it into a class. You need to put it into a class inside the class for such a small thing. That's too much! Provides internal classes, not to let you do such a thing, boring, not to let you do it. I think it may be because static members are similar to global variables in C language, and internal classes are usually used to create internal objects. Therefore, putting "global variables" in an internal class is meaningless. Since it is meaningless, it should be disabled.) Internal classes can directly access member variables in an external class, the internal class can be defined outside the external class's method, or in the external class's method body, as shown below:
Public class Outer {int out_x = 0; Public void method () {inner1 inner1 = new inner1 (); public class inner2 // internal class defined inside the method body {public method () {out_x = 3 ;}} inner2 inner2 = new inner2 ();} public class inner1 // internal class defined outside the method body {}}

 


The access types of internal classes defined outside the method body can be public, protecte, default, private, and so on, this is like the four access types of the member variables defined in the class. They determine whether the internal class definition is visible to other classes. In this case, you can also create instance objects of internal classes. When creating instance objects of internal classes, you must first create instance objects of external classes, then, use the instance object of this external class to create the Instance Object of the internal class. The Code is as follows:
Outer outer = new outer ();
Outer. inner1 inner1 = outer. New innner1 ();

There cannot be an access type modifier before the internal class defined inside the method, just like the local variable defined in the method, but the final or abstract modifier can be used before the internal class. This internal class is invisible to other classes. Other classes cannot reference this internal class, but instance objects created by this internal class can be passed to other classes for access. This internal class must be defined first and then used. That is, the definition code of the internal class must appear before the class is used, this is the same as that used after defining local variables in the method. This internal class can access local variables in the method body, but the final modifier must be added before the local variable.
For these details, you only need to try writing code in eclipse and learn all kinds of error information prompted by the development tool immediately.

You can also use the following syntax inside the method body to create an anonymous internal class, that is, when defining an interface or a subclass of a class, you also create an instance object for this subclass, you do not need to define a name for this subclass:
  public class Outer    {            public void start()            {                new Thread(           new Runable(){                public void run(){};       }         ).start();            }    }

 



Finally, you can add the static keyword before the internal class defined externally by the method to become the static nested class. It no longer has the characteristics of the internal class. All, in a narrow sense, it is not an internal class. There is no difference between static nested class and normal class in the behavior and function at runtime, but there are some differences in syntax during programming reference, it can be defined as public, protected, default, private, and other types, while common classes can only be defined as public and default. The name of the static nested class referenced outside is "external class name. Internal class name ". You can directly create a static nested class without creating an instance object of an external class. For example, if Inner is a static nested class defined in the outer class, you can use the following statement to create an inner class:
Outer. Inner inner = new outer. Inner ();
Because static nested class does not depend on instance objects of external classes, static nested class can access non-static member variables of external classes. When accessing 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 external class, in the static nested class, you can also directly reference the static member variables of the external class without the external class name.
The internal class defined in the static method is also the static nested class. At this time, the static keyword cannot be added before the class. The static nested class in the static method is similar to the internal class application method in the common method, in addition to the static member variables in the external class, it can also access the local variables in the static method. However, the final modifier must be added before the local variable.

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.