| Back to logs list
Reprinted from 304510237 at 10:37 on July 23, 2010 Reading (loading. ..) Comments (0) Category: Technical Articles
in a class to create another class, called members of the inner classes. This member can be a static inner class (using the static keyword modification), or non-static. Because the static inner class in the definition, use when there will be various restrictions. So in practical work is not much used.
in the development process, the internal class used in the most member or non-static inner classes. However, in certain circumstances, static inner classes can also play its unique role.
First, the purpose of use static inner classes.
inner classes in the definition of the time, you can add a permission in front of modifier static. At this point it becomes a static inner classes within the class. However, because of various reasons, such as restrictions on the use of more factors (the use of specific restrictions in the following sections I will detail),
nike dunks, is not used in a lot of practical work. But not to say that it has no value. In some special cases,
dunk sb shoes, having this really is not a static inner class. Carrying out the procedures for testing the code, if a Java source file in each set in both a primary method (primary method is the entrance of an application, you must have), then there will be a lot of extra code. And when this most important code for the Java main program file, it is only a form,
dunk sb shoes sale, does not in itself require that the main method. But less is the main method is never do. In this case, it can be written to the static main method within the class, so no Java source file for each of a similar set main method. This test is very useful code. In a number of large-scale application development, it is a commonly used technical means. To this end, the static inner classes though not very common, but the program developers also need to master it. Perhaps at some critical moment, its also a great role to play instead.
Second, restrictions on the use of static inner classes.
the inner class is defined as a static class, like the other class is defined as a static class method is basically the same, the reference rules are basically the same. But the details are still very different. Specifically, there are few places to cause you as program development officers.
one static members (including the static variables and static member) of the definition. In general, if an inner class instead of being defined as a static inner class, then define a member variable or member of the method, can not be defined as static member variables and static member method. In other words, non-static inner classes can not declare static members. If a student in the class is now an internal classes defined age, if not the modification of the class using static keyword, that is not defined as a static class, then this class if you want to use the internal keyword to modify a static member method or field is not allowed. At compile time to pass. Therefore, application developers need to pay attention only to the internal class modifier is a static class, then this class can define static member variables and member methods. This is a static inner class has a feature. It is also for this reason, sometimes less the static inner classes, a lot of work can not be completed. Or around a large circle to be able to achieve a user's needs. This is also the reason why static inner class is an important reason to exist.
Second, in reference to members, there are relatively large limits. General non-static inner classes, free access to the external class member variables and member methods. Even if these members of the method is modified as private (private member variable or method), its non-static inner class can freely access. The non-static inner class privileges. Because in other classes can not access is defined as a private member variable, or the method. However, if an internal class was defined as static, then the silver with an external member of a class method or when the member variables, there will be many restrictions. If not within the class from a static object to access the external non-static class members (including members of member variables and methods). What does this mean? If the external class defines two variables, a non-static variable, a static variable. Then the static inner class, method, whether inside or in the member elsewhere, only able to refer to an external static class variables, and not be able to access non-static variables. In the static inner class, you can define a static method (only in static inner classes can be defined in a static method), referenced in the static method of external members of the class. But no matter where in the internal class reference, there is one thing in common, that is only able to refer to members of the external class method or static member variables. For those non-static member variables and member methods, static inner classes can not be accessed. This is the maximum static inner class use restrictions. In ordinary non-static inner classes do not have this restriction. It is also for this reason, the decision of the static inner class is only used in certain occasions. Its scope of application as far as non-static inner classes extensively.
Third, when creating a static inner class does not need to bind an instance of a static inner class instances of class outside.
Typically, members of a class to create an internal class, there is a mandatory requirement that the internal class instance must be bound in the outer class instance. In other words, create the inner class outside the class before the first use of new keyword to create the inner class object. If so, then initialize a class from outside the object within the class, then the inner class object is bound to the outer class object. In other words, the general non-static inner class object is attached to the outer class object into the. However, if members of the developers time to create a static inner class, then this is another matter. Typically, programmers define static inner class, is not defined in the external binding on the instance of the class. That is, an external class to define a static inner classes do not need to use keywords to create a new instance of inner class. Static class that is created within the object, not the outer class object. Why is it so specific, the general application developers do not need to know so deep, just remember to have this rule. Static inner classes in the definition of the time, do not commit unnecessary mistakes.
From the above analysis we can see, static inner classes with non-static inner classes have a lot of different. General procedures so developers can understand the internal class object amorphous implicitly stored in the external class, a reference to the outer class object that created it. Regardless of such understanding, application developers need to bear in mind the non-static inner classes static inner classes with the difference. Such as whether members can create static methods and member variables (static inner class can not create a static member can not be static inner classes), for access to external restrictions on members of the class (static inner classes can only access the external static class member variables and member methods rather than static inner classes can access the static that can access non-static members of outer class member methods and variables). These two differences are non-static inner classes with the largest difference between the static outer class is a static inner class exists reasons. Understand this difference, the program developers also need to know under what circumstances the use of a static inner classes. If the program test, in order to avoid the Java source files in all the main methods of writing code, you can write to the main method static inner classes to reduce the amount of code written, make the code more concise.
short,
nike dunk, static inner classes in the Java language is a very special class, with the ordinary static class and non-static inner classes are very different. As a program developer, need to know the difference between them, and in practical work where appropriate to use the appropriate class. But overall, static inner classes is not very high frequency of use. However,
cheap dunk sb, in some cases, without this internal static class,
high nike dunk, it may not achieve much of the negative effects will play
static inner class can access only the static outer class member variable or method, in an external static inner classes to create a direct reference to an external class name, rather than create an instance of the outer class reference (non-static inner classes require First create an external class), because the latter reason, which leads to the former rules.