Static class in Java
In general, you can't modify a class with static. If you must use static to modify a class, static usually modifies an anonymous inner class.
Create another class in one class, called the member inner class. The inner class of this member can be static (decorated with the static keyword) or non static. Because there are various restrictions on the definition and use of the static inner class, it is not used much in practical work.
In the development process, the most used internal classes are non static member internal classes. However, static inner classes can also play a unique role in specific situations.
1、 Purpose of static inner class.
When defining an internal class, you can prefix it with a permission modifier static. At this point, the inner class becomes a static inner class. However, due to various reasons, such as restrictions on use and other factors (the specific restrictions on use will be described in detail in the following contents), they are not used very much in practical work. But that's not to say it's worthless. In some special cases, it's really impossible without this static inner class. For example, during code program testing, if a main method is set in each java source file (the main method is the entry of an application and must be provided), a lot of additional code will appear. And the most important thing is that the code of this main program is only a form for java files, and it itself does not need this main method. But without this main method, it is absolutely impossible. In this case, you can write the main method into the static inner class, so you don't have to set a similar main method for each java source file. This is very useful for code testing. It is a common technical means in some medium and large-scale application development. For this reason, although this static internal class is not commonly used, program developers must also master it. Maybe at a critical moment, it can also play a great role.
2、 Restrictions on the use of static inner 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 basically the same. However, the details are still very different. Specifically, there are the following places to attract the attention of all program developers.
One is the definition of static members (including static variables and static members). In general, if an internal class is not defined as a static internal class, it cannot be defined as static member variables and static member methods when defining member variables or member methods. That is, static members cannot be declared in non static inner classes. For example, an internal class age is defined in a student class. If this class is not modified with the static keyword, that is, it is not defined as a static class, it is not allowed to modify a member method or member variable with the static keyword in this internal class. It won't pass at compile time. Therefore, program developers should note that only when an internal class is modified into a static class, can static member variables and member methods be defined in this class. This is a feature of static inner classes. For this reason, sometimes without this static internal class, a lot of work cannot be completed. In other words, it takes a big circle to meet the needs of a user. This is also an important reason why static inner classes exist.
Second, there are relatively large restrictions on the reference of members. General non static internal classes can freely access member variables and member methods in external classes. Even if these member methods are modified as private (private member variables or methods), their non static internal classes can be accessed at will. Is the privilege of a non static inner class. Because member variables or methods defined as private cannot be accessed in other classes. However, if an internal class is defined as static, there will be many restrictions when using member methods or member variables of external classes. For example, non static members of the external class (including member variables and member methods) cannot be accessed from the object of the static internal class. What does that mean? If two variables are defined in the external class, one is a non static variable and the other is a static variable. Then, in the static inner class, no matter inside the member method or elsewhere, you can only reference the static variables in the external class, but not access the non static variables. In static inner classes, you can define static methods (and only static methods can be defined in static inner classes), and reference members of external classes in static methods. However, no matter where the internal class is referenced, it has one thing in common, that is, it can only refer to static member methods or member variables in the external class. For non static member variables and member methods, they are inaccessible in static internal classes. This is the maximum usage limit for static inner classes. There is no such restriction in ordinary non static inner classes. It is for this reason that static inner classes are only used in some specific situations. Its application scope is far less extensive than that of 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 an internal class of a member in a class, there is a mandatory provision that the instance of the internal class must be bound to the instance of the external class. In other words, before creating an internal class, use the new keyword in the external class to create the object of the internal class. In this way, if an inner class object is initialized from an outer class, the inner class object will be bound to the outer class object. In other words, the objects of ordinary non static internal classes are attached to external class objects. However, if a member developer creates a static inner class, this is another matter. Usually, when programmers define static internal classes, they do not need to define instances bound to external classes. In other words, 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 you create an internal object of a static class, you do not need an object of its external class.
newMainInStaticClass. Main();
Specific why this happens, general program developers do not need to understand so deeply, just remember this rule. When defining static inner classes, don't make the mistake of adding to the snake.
From the above analysis, it can be seen that static internal classes are very different from non static internal classes. General program developers can understand that a non static internal class object implicitly saves a reference in the external class, pointing to the external class object that created it. Regardless of this understanding, program developers need to keep in mind the differences between static internal classes and non static internal classes. For example, whether static member methods and member variables can be created (static internal classes can create static members, but not static internal classes) Restrictions on accessing members of external classes (static internal classes can only access static member variables and member methods in external classes, rather than static internal classes, that is, they can access static or non static external class member methods and member variables). These two differences are the biggest difference between static internal classes and non static external classes, and they are also the reason why static internal classes exist. After understanding this difference, program developers also need to know when to use static internal classes. For example, during program testing, in order 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 and make the code more concise.
In short, static inner class is a very special class in Java language, which is very different from ordinary static classes and non static inner classes. As a program developer, we must know the differences between them and adopt the appropriate classes in the right place in practical work. However, in general, the frequency of using static inner classes is not very high. However, on some occasions, without this internal static class, it may have the negative effect of getting twice the result with half the effort
3、 Instantiation
After reading the above content, I tested it as a whole:
First of all, we need to understand that static internal classes just don't depend on external classes, and its variables and methods don't have to be static. Let's take the code as an example:
The above code instantiates the static anonymous class with new. After running, enter 10 ABC, which is correct!
summary
The above is all about the detailed explanation of static class in Java. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!