Java Study Notes 20 --- adding internal classes to member Internal classes (1), Study Notes 20 ---

Source: Internet
Author: User

Java Study Notes 20 --- adding internal classes to member Internal classes (1), Study Notes 20 ---

In the previous article, note 19 briefly introduced the internal class, local internal class, and anonymous internal class of the member, and added some content to the internal class of the member.

There are mainly the following six points:

1. The member's internal class cannot have static members, except when the member variable is static final.

2. External classes cannot directly access member variables of member Internal classes or call Member methods of member Internal classes.

3. The member internal class can access the member variables of the external class without restriction, and call the member methods of the external class.

4. When a member of the internal class has the same name as an external class member, the internal class member will block the member with the same name as the external class member.

5. The internal class of the member can be modified by the access permission modifier.

6. The member variables and member methods of the member's internal classes can also be modified by the access permission modifier.

(Note: It's a bit late. Write five or six articles later)

 

Author: Chan

Please respect the author's Labor achievements. for reprinting, please mark "reprinted" in the title and the original article link:

Http://www.cnblogs.com/chanchan/p/8254124.html

 

The descriptions are as follows:

 

1. The member's internal class cannot have static members, except for the member variable static final.

Example:

1 // note 19: internal class-member Internal class-no static member is allowed, except for 2 class InnerClass {3 String name = "li" when the member variable is static final "; 4 String inname; 5 static String ingender = "female"; 6 7 // No static member is allowed unless declared as static final 8 void testStFi () {9 System. out. println ("ingender:" + ingender); 10} 11 12 // The static member Method 13 static void testStFiMeth () {14 System. out. println ("static member method of the member internal class"); 15} 16} 17 18 public static void main (String [] args) {19 20 Person per = new Person (); 21 Person. innerClass inC = per. new InnerClass (); 22 23 // note 19: internal class -- member internal class -- 4 cannot have static members unless declared as static final24 inC. testStFi (); 25}

 

The following error is reported during compilation:

Exception in thread "main" java. lang. Error: Unresolved compilation problems:
The field ingender cannot be declared static in a non-static inner type, unless initialized with a constant expression
The method testStFiMeth cannot be declared static; static methods can only be declared in a static or top level type

 

Analysis:

(1) According to the error message, the member's internal class cannot have static member variables, unless the Member is initialized by a constant expression;

As you know, the value of a member variable modified by static final cannot be changed,Is the compile time,Therefore

StaticString ingender = "female ";

Changed:

Static finalString ingender = "female ";

Compile the code again and you will be able to pass it.

(2) Why cannot a static member variable exist in a Member's internal class? 

1). For external classes,When the class has not been loaded, accessing static member variables or creating an object can cause class loading.When the class is loaded, space is allocated for static member variables. When accessing its static member variables, if the class has not been loaded, the class will be loaded first, and the static member variables will be allocated space for access. If the class has already been loaded, it indicates that the static member variables have been allocated with memory and can be accessed directly. Therefore, an external class can correctly access its static member variables under any circumstances. (Refer to Note 11)

2). For member Internal classes, external classes are not automatically loaded during loading;Only when an object of an external class creates an object of the member's internal class for the first time will it cause loading of the member's internal class.Suppose a member's internal class has a static member variable. What if you need to access its static member variable before the Member's internal class is loaded? Obviously, before the class is loaded, its static member variables are not allocated memory.If you want to access a variable that does not exist, an error will occur.Therefore, the internal class of a member cannot have static member variables and static member methods (similar to the principle ).

Reference images are more intuitive:

(3). Extension: The External class and the internal class of the member are loaded differently from the parent class and subclass.

1 ). the internal class and external class of a member are affiliated and affiliated. You must use an external class object to create an object of the internal class of the member. Therefore, you can load the external class first, load the internal class of the member when necessary. An external class object can create multiple internal class objects, which is a one-to-many relationship. Recently, before going to bed every day, the girl had to listen to "clay dolls", "clay dolls, clay dolls, a clay doll, the same eyebrows, the same eyes, and the eyes won't blink ...". A clay doll is like a person, but has something less than a person. In this way, a person is regarded as an external class. A clay doll is a member of a person's internal class. Haha, a person creates a clay doll, in addition, it can create a variety of clay dolls.

2). The relationship between the subclass and the parent class is-a. The two classes are independent of each other, but they can be seen as a parent class object hidden in the subclass object. When using a subclass for the first time, you must first load the parent class and allocate memory to its static member variables, then load the subclass and allocate memory to its static member variables, and then initialize the member variables in sequence, call the constructor and so on (refer to Note 11 ). Therefore, the Parent and Child classes must be loaded here.

See the following link for details:

 

 

2. External classes cannot directly access member variables of member Internal classes or call Member methods of member Internal classes.

Example:

Class Person defines the InnerClass of the member's internal class. The member variable inname of the Person member method outerCAccessinC to access InnerClass.

1 // member Internal class 2 class InnerClass {3 String name = "li"; 4 String inname; 5 6 void printInC () {7 System. out. println ("inner class"); 8} 9} 10 11 // note 19: internal class -- member internal class -- Can external class access internal class member variables and call Member methods? 12 public void outerCAccessinC () {13 System. out. println ("inname:" + inname); 14} 15 16 public static void main (String [] args) {17 18 Person per = new Person (); 19 20 // note 19: internal class -- member internal class -- External class cannot access internal class member variables or call Member Method 21 per. outerCAccessinC (); 22 per. printInC (); 23}

 

The following error occurs:

The following error is prompted in row 13th:

  Inname cannot be resolved to a variable

The following error is prompted in Row 22:

  The method printInC () is undefined for the type Person

 

This indicates that the external class cannot directly access the member variables or member methods of the member's internal class.

 

On the other hand, if the external class can directly access members of the member's internal class and the above program is used as an example, create a Person Class Object per, and call the method outerCAccessinC by per, because no internal class object has been created, and no space is allocated for its member variables, then per will access a non-existent variable inname, which is not allowed.

Therefore, members of a Member's internal class are invisible to the external class, and the external class cannot directly access members of the member's internal class.

 

3. The member internal class can access the member variables of the external class without restriction and call the member methods of the external class.

Example:

Class Person defines several member variables and a member's internal class InnerClass. The member InnerClass defines the member method testVarMeth and testVarMeth to access and call multiple member variables and member methods of the external class Person.

String name; int age; String gender; public String education; // anti-question permission modifier private String holobby; protected String residence; static String citizenship = "Chinese "; class InnerClass {void testVarMeth () {System. out. println ("name:" + name); System. out. println ("education:" + education); System. out. println ("holobby:" + holobby); System. out. println ("residence:" + residence); System. out. println ("citizenship:" + citizenship); System. out. println ("age:" + getAge (); System. out. println ("gender:" + getGender () ;}} public static void main (String [] args) {Person per = new Person (); Person. innerClass inC = per. new InnerClass (); // note 19: internal class -- member internal class -- Internal class access external class member variable, call Member method inC. testVarMeth ();}

 

Output result:

name:nulleducation:nullhobby:nullresidence:nullcitizenship:Chineseage:0gender:null

 

The result shows that,The internal class of a member can access or call the member variables or member methods of an external class at will, which has nothing to do with the access permission and type.

 

4. When a member of the internal class has the same name as an external class member, the internal class member will block the member with the same name as the external class member.

Example:

Slightly modify the program in 2 to add a member variable name with the same name as Person to the InnerClass of the member:

String name = "li ";

When the program is executed, the output result is:

name:lieducation:nullhobby:nullresidence:nullcitizenship:Chineseage:0gender:null

 

As you can see,If the member's internal class has a member with the same name as the external class, the member's internal class will shield the member with the same name, that is, members with the same name of the external class are invisible to the member's internal 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.