Static class (static class) in Java)

Source: Internet
Author: User

From: http://klcwt.iteye.com/blog/452834


Create another class in a class called the internal class of the member. The internal class of this Member can be static (modified using the static keyword) or non-static. Static internal classes have various restrictions during definition and use. Therefore, it is not much used in actual work.

During the development process, the most used internal classes are non-static members. However, under specific circumstances, static internal classes can also play their unique roles.

1. Use of static internal classes.

When defining an internal class, you can add a permission modifier static before it. This internal class is changed to a static internal class. However, for various reasons, such as restrictions on use and so on (the specific restrictions are described in detail in the following content), not many are used in practical work. But it does not mean that it has no value. In some special cases, it is not possible to lose this static internal class. For example, during code program testing, if you set a primary method in each Java source file (the primary method is the entry to an application, which must be included ), there will be a lot of additional code. In addition, the code of this main program is only a form for java files, and does not need this main method. However, it is absolutely impossible to lose the master method. In this case, you can write the primary method to the static internal class, so that you do not need to set a similar primary method for each Java source file. This is very useful for code testing. In some medium and large application development, it is a common technical means. For this reason, although this static internal class is not very common, it must be mastered by developers. Maybe it can play a huge role at a critical moment.

Ii. Restrictions on the Use of static internal classes.

Defining an internal class as a static class is basically the same as defining other classes as static classes, and the reference rules are also basically the same. However, the details are still quite different. Specifically, there are several main points to attract the attention of developers.

One is the definition of static members (including static variables and static members. Generally, if an internal class is not defined as a static internal class, when defining a member variable or member method, cannot be defined as static member variables or static member methods. In other words, static members cannot be declared in non-static internal classes. For example, if an internal class age is defined in a student class, if the class is not modified using the static keyword, It is not defined as a static class, it is not allowed to modify a member method or member variable by using the static keyword in this internal class. It won't work during compilation. Therefore, program developers must note that only by modifying an internal class to a static class can static member variables and member methods be defined in this class. This is a feature of all static internal classes. It is precisely for this reason that, sometimes, with this static internal class missing, a lot of work cannot be done. In other words, it is necessary to bypass a large circle to meet the needs of a user. This is also an important reason why static internal classes exist.

Second, there are large restrictions on member references. Generally, non-static internal classes can access member variables and member methods in external classes at will. Even if these member methods are modified to private (Private member variables or methods), their non-static internal classes can be freely accessed. It is a non-static internal Class privilege. In other classes, it is impossible to access the member variables or the method defined as private. However, if an internal class is defined as static, there will be many restrictions when Silver uses the member method of the external class or the member variable. For example, you cannot access non-static members of an external class (including member variables and member methods) from objects of static internal classes ). What does this mean? If two variables are defined in the external class, one is non-static and the other is static. In a static internal class, static variables in the external class can only be referenced inside the member method or elsewhere, but non-static variables cannot be accessed. In static internal classes, static methods can be defined (or static methods can be defined only in static internal classes), and members of external classes can be referenced in static methods. However, no matter where the internal class is referenced, there is one thing in common, that is, it can only reference static member methods or member variables in the external class. Non-static member variables and member methods cannot be accessed in static internal classes. This is the maximum usage limit for static internal classes. There is no such restriction in normal non-static internal classes. This also determines that static internal classes are only used in some specific scenarios. Its application scope is far from as wide as non-static internal classes.

Third, when creating a static internal class, you do not need to bind the instance of the static internal class to the instance of the external class.

Generally, when creating a member internal class in a class, there is a mandatory rule that the instance of the internal class must be bound to the instance of the external class. That is to say, before creating an internal class, you must use the new keyword in the external class to create the object of this internal class. In this case, if an internal class object is initialized from an external class, the internal class object will be bound to the external class object. That is to say, objects of common non-static internal classes are attached to external class objects. However, if a member developer creates a static internal class, this is another matter. Generally, when defining static internal classes, programmers do not need to define instances bound to external classes. That is to say, to define a static internal class in an external class, you do not need to use the keyword new to create an instance of the internal class. That is, when creating a static class internal object, you do not need the object of its external class. The specific reason is as follows. Generally, developers do not need to understand this details, but remember to have this rule. Do not make mistakes when defining static internal classes.

From the above analysis, we can see that static internal classes are quite different from non-static internal classes. Generally, programmers can understand that an amorphous internal Class Object implicitly stores a reference in the external class, pointing to the external class object that creates it. With this understanding, developers must remember the differences between static and non-static internal classes. For example, whether static member methods and member variables can be created (static internal classes can be created but not static internal classes) limitations on access to members of external classes (static internal classes can only access static member variables and member methods in external classes, rather than static internal classes. static external class member methods and member variables ). These two differences are the biggest differences between static internal classes and non-static external classes, and are also the reason why static internal classes exist. After understanding the difference, the program developers also need to know under what circumstances should they use static internal classes. For example, during program testing, to avoid writing the code of the main method in each Java source file, you can write the main method into the static internal class to reduce the amount of code writing, make the code more concise.

In short, static internal classes are a special class in the Java language, which is very different from common static classes and non-static internal classes. As a program developer, you must know the differences between them and adopt appropriate classes in the actual work. But in general, the usage frequency of static internal classes is not very high. However, in some cases, if this internal static class is not used, it may play a negative role.

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.