Java: Differences and linkages between inner classes and external classes

Source: Internet
Author: User
Tags object object

Note One: You can use the member methods and member variables of the outer class at will in the inner class.

   It is well known that when you define a member method or member variable, you can add some permissions to the modifier to prevent other classes from accessing it. If you precede a member variable or a member method with the Private keyword, other classes cannot call member methods or member variables in this class. However, if the class has member inner classes, this is not a limitation. In other words, member methods and member variables of external classes can be arbitrarily referenced within a member's inner class, even if the class member method or member variable is decorated with private. If an I variable is defined in a member's outer class, it is decorated with the private keyword. It is not possible to refer to this private member variable in other classes at this time. However, in the method of the member inner class of this class, you can not only refer to the private variable of this external class, but also can assign value and so on. This assignment operation is also valid for external classes. 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 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. Ordinary classes, when created, do not necessarily need to be instantiated immediately. When this object needs to be used, it can be instantiated again. However, if a class becomes a member inner class 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 on an instance of an external class. To put it simply, when defining an external class, if there is a member inner class, then do not look at the use of the new keyword in an external class to instantiate a reference to an inner class object. For external classes, you can instantiate them when you need them. This guarantees that an object of the inner class is created at the same time that an object is created with an external class. This guarantees that an instance of an inner class is bound to an instance of an external class.

Note three: Member methods and member variables are private in the members inner class.

as a member inner class, you can arbitrarily reference member variables and member methods in external classes. So is the member variable defined in the member's inner class, and can the external class be freely accessible? The answer is No. An inner class can access the members of its outer class, but members of the inner class (such as member variables or member methods) are known only within the scope of the inner class and cannot be referenced directly by the outer class. If a variable i is now defined in an external 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. However, in an external class, you cannot directly refer to a member variable in an inner class. For example, in an external class, using the Ii=5 assignment statement to change the value of this variable is a syntax error, and an error occurs at compile time. If the outer class really wants to refer to the members of the inner class, it is not a no-no. Just because you cannot make a direct reference, you can invoke the member variable of the inner class by using the method referenced by the inner class object. This is a point that the program developer needs to remember. Member inner classes and external classes access each other's member method restrictions differently. It is important to note 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 formulate the type of this object. It's very troublesome. To do this, try to avoid instantiating the inner class object outside of the outer class and non-static methods. Furthermore, the inner class object will depend on the outer class object, unless an outer class object already exists, otherwise the inner class object will not appear in the class. Simply put, the relationship between the inner class object and the Outer class object is very close. Sometimes it is like a pair of father and son (members of the inner class are free to use members of the outer class), and sometimes as strangers (external classes cannot directly use members from inner classes). As an emerging program developer, you must have an in-depth understanding of the relationship between an external class object and an inner class object. Because in real work, it is common to define member inner classes in an external class. Only by understanding their relationships with each other, in writing applications, can they be put to good control.

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

Variables can be defined in both the outer class and the member inner class. The member inner class can access variables in the outer class at will, while the outer class cannot directly access the variables in the inner class, only by referencing the member variables of the inner class by using an inner class object. However, it is important to note that variables defined in an external class can be the same as the names of variables defined in the inner class. In other words, a variable I can be defined in an external class, and a variable I can be defined in an inner class. Now the new problem comes out. Because the member methods and member variables in the outer class are freely accessible in the inner class. But now that the member inner class is the same as the variable name of the outer class, if you want to use the variable I in the inner class, how does the compiler know that you need to invoke the variable I of the external class or the variable I of the inner class?

Related Article

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.