The role of the Java static inner class

Source: Internet
Author: User


create another class in one class, called the member inner class. This member inner class can be static (with the static keyword decorated), or it can be non-static. Since static internal classes are defined and used, there are various limitations. So there is not much to be used in the actual work.  
In the development process, the most or non-static member inner class used in the inner class. However, in certain situations, static internal classes can also play a unique role.  
first, the use of static internal classes purposes.  

      when defining an inner class, you can precede it with a permission modifier static. At this point, the inner class becomes a static inner class. However, due to various reasons, such as the use of restrictions and other factors (specific use restrictions, the author will elaborate in the following content), in the actual work is not a lot. But it is not that it has no value. In some special cases, the lack of this static inner class is really not good. 


The following example shows:

public class Student {
//...... Code
}
public class Person{
/ / name
private String name;  
/ / family
private Home home;  
//Constructor set property value
public Person(String _name){  
name = _name;  
}
/*Omit getter / setter methods of home and name*/
public void setHome(Home home){
this.home = home;
}
public Home getHome(){
Return home;
}
public static class Home{  
//Home address
private String address;  
//Home phone
private String tel;  
public Home(String _address,String _tel){  
address = _address;  
tel = _tel;  
}
/*The getter / setter methods of address and Tel are omitted*/
public static void main(String[] args) {  
Student student = new Student();
}
}
}





second, the use of static internal class restrictions.  
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 consistent. However, the details are still very different. Specifically, there are a number of places to draw the attention of the developers of the program.  
one is the definition of static members, including static and static members. In general, if an inner class is not defined as a static inner class, it cannot be defined as a static member variable or a static member method when defining a member variable or a member method. In other words, static members cannot be declared in non-static inner classes. If an inner class age is now defined in a student class, it is not defined as a static class if the class is not decorated with the static keyword, so it is not allowed to use the static keyword to decorate a member method or member variable in this inner class. It's not going to work at compile time. Therefore, the program developers need to note that only an inner class is decorated as a static class, then you can define static member variables and member methods in this class. This is an attribute of the static inner class. It is for this reason that sometimes less of this static internal class, a lot of work can not be completed. Or to go around a big circle in order to achieve the needs of a user. This is also an important reason for the existence of static internal classes.  
II is on the member's reference, there is a relatively large limit. A general non-static inner class that can arbitrarily access member variables and member methods in an external class. Even if these member methods are decorated as private (a proprietary member variable or method), their non-static inner classes are freely accessible. is a non-static inner class privilege. Because member variables that are defined as private are inaccessible in other classes or methods. However, if an inner class is defined as static, there are a number of limitations when Silver uses the member method of the outer class or the member variable. Non-static members of external classes (including member variables and member methods) cannot be accessed from objects in the static inner class. What does that mean? If you define two variables in an external class, one is a non-static variable, and one is a static variable. In a static inner class, it is only possible to refer to static variables in the outer class, whether within the member method or elsewhere, without accessing non-static variables. In a static inner class, you can define a static method (and only static methods can be defined in a static inner class), referencing the members of an external class in a static method. However, there is one common denominator, regardless of where the inner class is referenced, that only static member methods or member variables in the outer class can be referenced. For non-static member variables and member methods, it is inaccessible in static inner classes. This is the maximum usage limit for static internal classes. There is no such restriction in ordinary non-static inner classes. It is for this reason that the static inner class is used only in some specific situations. Its scope of application is far from as wide as that of non-static internal classes.    
third, you do not need to bind an instance of a static inner class to an instance of an external class when creating a static inner class.  
in general, when creating a member's inner class in a class, there is a mandatory requirement that an instance of an inner class be bound to an instance of an external class. That is, before you create an inner class, you need to use the New keyword in the outer class to create an object of the inner class. In this case, if an inner class object is initialized from an external class, the inner class object is bound to the outer class object. In other words, the object of the ordinary non-static inner class is attached to the outer class object. However, if a member developer creates a static inner class, then this is another matter. Typically, when a programmer defines a static inner class, it is not necessary to define a binding on an instance of an external class. That is, to define a static inner class in an external class, you do not need to use the keyword new to create an instance of the inner class. That is, when you create a static class inner object, you do not need an object of its outer class. Specifically why this, the general program developers do not need to know so deep, just remember that there is this rule. When defining static internal classes, do not make the superfluous mistake.  
from the above analysis, it can be seen that static internal classes and non-static internal classes are still very different. As a general program developer can understand, a non-crystalline inner class object implicitly stores a reference in an external class, pointing to the outer class object that created it. Regardless of how it is understood, program developers need to keep in mind the differences between static internal classes and non-static inner classes. If you can create static member methods with member variables (static inner classes can create static members rather than static inner classes are not available), restrictions on accessing members of external classes ( Static inner classes can only access static member variables in external classes and member methods, but not static inner classes that can access static or access non-static external class member methods and member variables. These two differences are the biggest differences between static inner classes and non-static external classes, and are the reason why static inner classes exist. After understanding this discrepancy, the program developer also needs to know when to use static internal classes.  In order to avoid writing the code of the Main method in the various Java source files, you can write the main method to the static inner class to reduce the amount of code written and make the code more concise, as in the case of program testing.
in summary, static inner classes are a very special class in the Java language, and differ greatly from normal static classes and non-static inner classes. As a program developer, it is important to know the differences between them and to apply the appropriate classes in the right places in the actual work. In general, however, the use of static internal classes is not very high. But on some occasions, if there is no such internal static class, it may play a less effective negative effect.





The difference between a static inner class and a non-static inner class:





package com.devin;
public class MyMain{
private static String name = "woobo";
private String num = "X001";
//Static inner classes can be decorated with public, protected, and private
static class Person {
//Static or non static members can be defined in static inner classes
private String address = "China";
private static String x = "as";
Public string mail = "[email protected]; / / internal class public member
public void display() {
//System. Out. Println (Num); / / non static members of external classes cannot be accessed directly
//Static inner classes cannot access non static members of outer classes (including non static variables and non static methods)
System. Out. Println (name); / / only static members of external classes can be accessed directly
//Static inner classes can only access static members of outer classes (including static variables and static methods)
System. Out. Println ("inner" + address); / / access members of this inner class.
}
}
public void printInfo() {
Person person = new Person();
//External class accesses the non static members of internal class: just instantiate the internal class
person.display();
//System. Out. Println (mail); / / not accessible
//System. Out. Println (address); / / not accessible
System. Out. Println (person. Address); / / you can access private members of internal classes
System. Out. Println (person. X); / / external classes access static members of internal classes: internal classes. Static members
System. Out. Println (person. Mail); / / public members of internal classes can be accessed
}
public static void main(String[] args) {
MyMain staticTest = new MyMain();
staticTest.printInfo();
}
}









1. Objects of nested classes do not require objects of their enclosing classes. That is, it can be instantiated without relying on an external class instance.



2. Non-static perimeter class objects cannot be accessed from objects in the nested class. This is defined by the Java syntax in which "static methods cannot access non-static members directly"



3. The external class accesses the members of the inner class in particular and cannot be accessed directly, but can be accessed through an internal class instance because all members and methods within the static nesting default to static. Also note that the inner static class person is only visible within the class Statictest scope, It is wrong to refer to or initialize in other classes.
4. Static inner classes can have static members, and non-static inner classes cannot have static members.
5. A non-static member of a static inner class can access the static variables of the outer class, and cannot access the non-static variables of the outer class;



6. Non-static members of non-static inner classes can access non-static variables of the outer class.



Generating a static inner class does not require an external class member: This is the difference between a static inner class and a member inner class. Objects of a static inner class can be generated directly: Outer.Inner in = new Outer.Inner (), rather than generated by generating an external class object. This actually makes the static inner Class A top class (under normal circumstances, you cannot place any code inside the interface, but the nested class can be part of the interface because it is static.) Just place the nested class inside the interface's namespace, which does not violate the rules of the interface)



Static in Java is typically used to modify member variables or functions. But there is a special use of static to decorate the inner class, the ordinary class is not allowed to declare static, only the inner class can be. An inner class that is modified by static can be used directly as a normal class without the need to instantiate an external class (see code below):




public class OuterClass {
public static class InnerClass{
InnerClass(){
System. Out. Println ("=========== I am an internal class' innerclass' ===========");
}
}
}
public class TestStaticClass {
public static void main(String[] args) {
//Do not need a new outclass
new OuterClass.InnerClass();
}
}


If the interclass is not decorated with static, it can only be called as follows:






package inner_class;
public class OuterClass {
public class InnerClass{
InnerClass(){
System. Out. Println ("=========== I am an internal class' innerclass' ===========");
}
}
}
public class TestStaticClass {
public static void main(String[] args) {
//Outclass needs to be an example
OuterClass oc = new OuterClass();
oc.new InnerClass();
}
} 










The role of the Java static inner class


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.