Member internal class and static internal class

Source: Internet
Author: User

</Pre> <pre name = "code" class = "java"> public class Test {// java internal classes are divided: member internal class, static nested class, method internal class, anonymous internal class. // The member internal class and static nested class must be called in the external class. This article mainly analyzes the two types of internal external calls. // The internal class of the method can only be instantiated within the method that defines the internal class; the class object can be directly created when the internal class is defined anonymously. // Member internal class Test1 class Test1 {int I; public Test1 (int I) {this. I = I; System. out. println ("internal class 1 test");} public int fun1 () {return I ;}// member internal class Test2 class Test2 {int I; public Test2 (int I) {this. I = I; System. out. println ("internal class 2 test");} public int fun2 () {return I ;}// static internal class. Static or non-static members can be defined in static internal classes. // The static internal class cannot access non-static members or non-static methods of the external class. Static class Test3 {public int I = 4; public static int m = 3; public static void fun3 () {System. out. println (m);} public void fun4 () {System. out. println (m) ;}// a non-static method can call static or non-static variables or methods. Public void fun5 () {// for non-static methods, you can access the internal class of the member by using "new external class. new internal class" or "new internal class. // In non-static methods, the new member internal class type can be "external class. Internal class" or "internal class ". Test. test1 m = new Test1 (5); System. out. println (m. fun1 (); Test2 n = new Test (). new Test2 (10); System. out. println (n. fun2 (); // in non-static methods, for static internal classes, you can "internal classes. XX "or" external class. internal class. XX "or" new internal class "to access, you cannot use the new external class new internal class to access the static members or static methods in the static internal class using the non-static method. XX "access Test3.fun3 (); System. out. println (Test. test3.m); // use the new static internal class to access non-static members in the static internal class or non-static methods, and then access new Test3 (). fun4 (); System. out. println (new Test3 (). i); // new static The internal class type can be "external class. internal class or internal class Test. test3 tt = new Test3 (); tt. fun3 (); Test3 ttt = new Test3 (); ttt. fun4 ();} // The operation for accessing internal classes of members in static methods is similar to the operation for accessing non-static members or non-static methods in static methods, "new External class. new member internal class, and then access. // "Access to static internal classes in static methods" is similar to "Access to static members in static methods or operations in static methods. XX "call. // However, static internal classes can define non-static members and Methods. to access non-static members or non-static methods in static internal classes, you need to "new static internal class ", then access. Public static void main (String [] args) {// you cannot access the internal class of the member as follows. You must first create an external class // Test. test1 t4 = new Test1 (8); Test. test1 t1 = new Test (). new Test1 (8); System. out. println (t1.fun1 (); // The type of the new member internal class t2 in the main static method can be "external class. internal class, which can also be "internal class" Test2 t2 = new Test (). new Test2 (7); System. out. println (t2.fun2 (); Test t3 = new Test (); t3.fun5 (); // call static variables or static methods Test3.fun3 () by using static internal class names; System. out. println (Test. test3.m); // access the static internal class The non-static member or non-static method in requires a new static internal class, and then accesses the System. out. println (new Test3 (). i); new Test3 (). fun4 (); // report an exception // Test3.fun4 (); // The type of the new static internal class tt in the main static method can be "external class. internal class, which can also be "internal class" Test3 tt = new Test3 (); System. out. println (tt. i); Test. test3 ttt = new Test3 (); // The static method in the static internal class can also be "new static internal class. static Method. Ttt. fun3 ();}}


The internal class of the Member cannot contain static variables and methods. Because the internal class of a member needs to be created before it can be created.

Static internal classes cannot access non-static members or non-static methods of external classes. Static internal classes can include "static and non-static" members and methods.


Common internal classes cannot have static data or static attributes, nor can they contain Nested classes, but Nested classes can. The nested class cannot be declared as private. It is generally declared as public for convenient calling.
A java file can have only one public class, but one class can have multiple public internal classes.

Normal (non-Internal) classes cannot be set to private or protected, but only public (or "friendly"); internal classes can be public, private, protected, and friendly.

 

For member Internal classes: when an internal class object is created using an external class, this internal Class Object secretly captures a reference pointing to the external class ("external class. this ", when operating on external class variables or methods in the internal class, it can be added to the front or not written. If it is not written, the compiler will automatically recognize it, and the effect will be the same. By default, this reference will not be written ), therefore, you can use this reference to access members of an external class. Generally, these are all handled by the compiler, and we don't need to care about them. Because of this, when creating an internal class object, it must be associated with an external class object. In addition, the internal class can also directly access members of the external class.

You can use the "external class. this is used to obtain the reference of the peripheral class object used to create the internal class (generally, the internal class can access all the variables and methods in the external class without such access ). Of course, you can also "new external class" in the member's internal class. At this time, an external class object is re-created.


Summary:

Static internal class, we only need to "new internal class. xx "will not cause errors (but generally, when static internal classes are required, they will refer to" Internal classes "for static members or static methods in internal classes. XX ")

Internal class of the member. We only need "new external class. new internal class" to avoid errors.

As long as the general type of the new internal class is "external class. Internal class", no error occurs.


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.