Considerations for creating members ' inner classes

Source: Internet
Author: User
Tags reference

Note One: You can use the member methods of the external class as well as the member variables in the inner class.

It is well known that when defining a member method or a member variable, you can add some modifiers to it to prevent access to other classes. If you precede a member variable or member method, plus the private keyword, other classes cannot invoke the member methods or the member variables in that class. However, if the class has members ' inner classes, it is not limited by this. That is, you can arbitrarily reference the member methods of the outer class and the member variables in the member's inner class, even if the class member methods or member variables are decorated private. If you define an I variable in the member Outer class and use the private keyword to decorate it. It is not possible to reference this private member variable in other classes at this time. But in the method of the member inner class of this class, not only can the private variable of the outer class be referenced, but it can also be assigned a value. This assignment operation is also valid for the external class. That is, the member inner class can change the value of the private variable in the outer class.

Note two: To instantiate a reference to an inner class object in an external class.

If a class is defined in another class and becomes a member's inner class, it is important to note that an instance of an inner class must be bound to an instance of an external class. That is, to initialize an object of an inner class from an external class, the object of the inner class is bound to the object of the outer class. This is different from the normal class. Normal class, after creation, does not necessarily need to instantiate immediately. When you need to use this object, then instantiate it. However, if a class becomes a member of another class, it is different. You must instantiate a reference to an inner class object in an external class to implement binding an instance of an inner class to an instance of an external class. Simply put, when you define an external class, if you have a member inner class, you don't want to use the New keyword to instantiate a reference to an internal class object in an external class. For an external class, you can instantiate it when you need it. This makes it possible to create an object with an inner class while creating an object with an external class. This ensures that an instance of the inner class is bound to an instance of an external class.

Note Three: The privacy of member methods and member variables within a member's inner class.

As a member inner class, you can arbitrarily reference member variables and member methods in an external class. So is the member variable defined in the member's inner class, and can the outer class be accessed arbitrarily? The answer is No. An inner class can access members of its external class, but the members of the Inner class (such as member variables or member methods) are knowable only within the scope of the inner class and cannot be directly referenced by external classes. If you now define a variable i in the outer class, another variable II is defined in the inner class. In the member inner class, you can directly reference the variable I in the outer class, or you can assign it directly, such as i=5 and so on. In an external class, however, you cannot directly refer to the member variables in the inner class. In the outer class, the value of the variable is changed by using the II=5 assignment statement, which is a grammatical error and errors are found at compile time. If the outer class really wants to refer to the members of the inner class, it is not not. It is simply not possible to make a direct reference, but to use an internal class object reference method to invoke the member variables of the inner class. This program developer needs to remember. Member-Internal classes and external classes access each other's member method restrictions are different. It is particularly noteworthy that if you instantiate an internal object outside of an external class and a non-static method, you need to use an external class. The form of an inner class to make the type of this object. It's very troublesome. To do this, try to avoid instantiating internal class objects outside of external classes and Non-static methods. Furthermore, the inner class object will depend on the external class object, unless an external class object is already present, otherwise the inner class object will not appear in the class. Simply put, the relationship between internal class objects and external class objects is very close. Sometimes it's like a parent-child (Member-class can use members of an external class at will), sometimes like a stranger (an external class cannot directly use members in an inner class). As an emerging program developer, you must have an in-depth understanding of the relationship between external class objects and internal class objects. Because in actual work, it is common to define members ' inner classes in an external class. Only by understanding their relationship with each other, in writing applications, can they be controlled properly.

Note implementation Four: Use the This keyword to get references to internal classes and external class objects.

You can define variables in both the outer class and the member inner class. A member's inner class can access a variable in an external class at will, while an outer class cannot directly access a variable in the inner class, only by using an inner class object to refer to the member variable of the inner class. It should be noted, however, that the variables defined in the external class can be the same as the variable names defined in the inner class. That is, you can define a variable i in the outer class, and you can define a variable i in the inner class. At this point the new problem came out. Member methods and member variables in an external class are freely accessible in the inner class. But at this point the member inner class is the same as the variable name of the outer class, so if you want to use this variable I in the inner class, how does the compiler know that you now need to call the variable I of the outer class, or the variable I of the inner class?

In practical work, this situation should be avoided as far as possible. That is, when defining variable names, the name of the variable in the inner class is not as good as the variable name in the outer class. However, if this happens, the Java compiler still has the means to solve the conflict problem of the variable name. If you encounter a member variable name in a class that is the same as the name of a member variable in an external class, you can distinguish it by using the This keyword. As now there is a class name called student. In this class, a member's inner class is created, named age. Now each of these two classes defines a member variable i, which is used for looping purposes. How do you differentiate between calling this variable in a member's inner class? Typically, if you use THIS.I to invoke a variable, it means that the variable is a variable defined in the member's inner class. Calling a variable by using student.this.i means that the member variable of the outer class is referenced at this time. That is, if an inner class references a member of the same name in an external class, it needs to be called by an external class-fan name. this. The name of the external class member is referenced. Also, you need to use the This keyword to make a distinction when referencing members of your own definition within a member's inner class. Obviously it's very troublesome. To this end, I would like to stress again, unless there is a very special need, otherwise try to avoid two external classes and members of the internal class member variables or member methods using the same name. Otherwise, it will cause a lot of trouble for subsequent references.

As can be seen from the above summary, the relationship between the member inner class and the outer class is indeed very complex. As a Java program developer, you must understand the rules that internal classes and external class members refer to each other, and the relationship between internal class objects and external class objects. When writing code, try to avoid naming conflicts between the internal class and the member variable in the external class. If the duplicate name is true, learn to use this keyword to eliminate the conflict. The author of the above talk about some of the implementation is also some of the more abstract content, need developers in the actual work to understand.

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.