Under normal circumstances, static modification classes cannot be used. If you have to use static to modify the class, static usually modifies the anonymous internal class.
Create another class in one class, called the member inner class. This member internal class can be static (using static keyword modification) or non-static. Because static internal classes have various restrictions when defining and using them. So not much is used in actual work.
During the development process, the most common internal classes are used in internal classes that are non-statically members. However, in specific cases, static inner classes can also play their unique role.
1. Purpose of the use of static internal classes.
When defining an inner class, you can prefix it with a permission modifier static. At this time, this inner class becomes a static inner class. However, due to various reasons, such as usage restrictions and other factors (the specific usage restrictions are explained in detail in the following content), there are not many used in actual work. But it doesn't mean it has no value. In some special cases, it is really not possible to lack this static inner class. For example, when conducting code program testing, if a main method is set in each Java source file (the main method is the entrance to a certain application and must have it), then a lot of extra code will appear. And the most important thing is that the code of this main program is just a form for Java files, and it does not require this main method itself. But it is absolutely impossible to do without this main method. In this case, the main method can be written into the static inner class, so that there is no need to set a similar main method for each Java source file. This is very useful for code testing. In some medium and large application development, it is a commonly used technical means. For this reason, although this static internal class is not very commonly used, program developers must also master it. Perhaps at some critical moment, it can still play a huge role.
public class MainInStaticClass {static class Main{static void main() {//Write the main method to a static inner class, so there is no need to have this kind of main method for each source file new MainInStaticClass().print();}}public static void main(String[] args){new MainInStaticClass().print();}public void print(){System.out.println("main in static inner class");}}public class TestMain {public static void main(String[] args) {// TODO Auto-generated method stub// new MainInStaticClass().print();MainInStaticClass.Main.main();new MainInStaticClass.Main();}}2. Limitations on the use of static internal classes.
Defining an inner 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 mainly the following areas to attract the attention of all program developers.
One is the definition of static members (including static variables and static members). Generally speaking, if an inner class is not defined as a static inner class, then it cannot be defined as a static member variable and a static member method 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 now 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, then it is not allowed to modify a certain member method or member variable in this internal class. It won't be able to pass when compiling. Therefore, program developers need to note that only by modifying an inner class as a static class can static member variables and member methods be defined in this class. This is a feature that static inner classes have. It is precisely for this reason that sometimes many tasks cannot be completed without this static internal class. Or it is necessary to go around a big circle to achieve the needs of a certain user. This is also an important reason why static internals exist.
Second, there are relatively large restrictions on member citations. General non-static inner classes can access member variables and member methods in external classes at will. Even if these member methods are modified as private (private member variables or methods), their non-static inner classes can be accessed at will. It is a privilege of non-static inner classes. Because in other classes, member variables or methods defined as private are inaccessible. However, if an inner class is defined as static, there will be many restrictions when using member methods or member variables of the outer class. For example, non-static members of external classes (including member variables and member methods) cannot be accessed from objects of static inner class. What does this 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 a static inner class, whether inside a member method or elsewhere, it can only reference static variables in the outer class, and cannot access non-static variables. In static inner classes, static methods can be defined (and only static methods can be defined in static inner classes), and members of the external class can be referenced in static methods. However, no matter where the inner class is referenced, there is one thing in common, that is, it can only reference static member methods or member variables in the outer class. For non-static member variables and member methods, they are inaccessible in static inner classes. This is the maximum usage limit for static inner classes. There is no such restriction in ordinary non-static inner classes. It is precisely this reason that static internal classes are only used in some specific occasions. Its application range is far less extensive than that of non-static internal classes.
Third, when creating a static inner class, there is no need to bind instances of the static inner class to instances of the external class.
Generally speaking, when creating a member internal class in a class, there is a mandatory provision, that is, instances of inner class must be bound to instances of outer class. That is to say, before creating an inner class, you must first use the new keyword in the outer class to create an object of this inner class. In this case, if an inner class object is initialized from the external class, the inner class object will be bound to the outer class object. In other words, objects of ordinary non-static inner classes are attached to external objects. However, if the member developers create static inner classes, that's another matter. Generally speaking, when a programmer defines a static inner class, he does not need to define instances of the external class. That is to say, to define a static inner class in an external class, there is no need to use the keyword new to create an instance of the inner class. That is, when creating an internal object of a static class, objects of its external class are not needed.
newMainInStaticClass.Main();
For the specific reason, generally program developers do not need to understand so deeply, they just need to remember that there is this rule. When defining static internal classes, never make the mistake of adding extra money.
From the above analysis, we can see that static inner classes are still very different from non-static inner classes. Generally, program developers can understand that non-static inner class objects implicitly save a reference in the outer class to point to the outer class object that created it. Regardless of this understanding, program developers need to keep in mind the differences between static inner classes and non-static inner classes. For example, whether static member methods and member variables can be created (static inner classes can create static members instead of static inner classes cannot), restrictions on accessing members of external classes (static inner classes can only access static member variables and member methods in external classes instead of static inner 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 inner classes and non-static outer classes, and are also the reason why static inner classes exist. After understanding this difference, program developers also need to know under what circumstances should they use static inner classes. For example, during program testing, in order to avoid writing the main method code in each Java source file, the main method can be written into a static internal class to reduce the amount of code written and make the code more concise.
In short, static inner classes are a very special class in the Java language, which is very different from ordinary static classes and non-static inner classes. As a program developer, it is necessary to know the differences between them and adopt the right classes where they are in real work. However, in general, the frequency of static internal classes is not very high. However, in some occasions, if this internal static class is not available, it may have a negative effect of half the result with twice the effort.
3. Instantiation
After reading the above content, I tested it as a whole:
First of all, everyone needs to understand that static inner classes are just not dependent on external classes, and the variables and methods inside them are not necessarily static. Let’s talk about the code below:
public class Test {public static void main(String[] args){MyInnerStaticClass inner=new MyInnerStaticClass();inner.a=10;inner.b="abc";System.out.println(inner.a+" "+inner.b);}static class MyInnerStaticClass{int a;String b;}} public class Test {public static void main(String[] args){MyInnerStaticClass inner=new MyInnerStaticClass();inner.a=10;inner.b="abc";System.out.println(inner.a+" "+inner.b);}static class MyInnerStaticClass{int a;String b;}}The above code instantiates the static anonymous class with new, enter 10 abc after running, correct!
Summarize
The above is all the detailed explanation of the static class in java in this article. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!