Java internal class _ makes you understand at a Glance _ no longer need to be confused

Source: Internet
Author: User

In a blog: "Analysis of the Role of Java internal classes", you can understand some of the Java internal classes, but there are some points in the internal classes that deserve our careful research...

The following are some of the Java internal classes I have summarized and shared with everyone ....

 
One: static internal classes can have static members, but non-static internal classes cannot have static members.

How can we understand this?

Take a look at the followingCode:

 1   /**  2   *  3    */  4   Package  Com. b510.test;  5   6   Public  Class  Test {  7       Private   Int Number = 1 ;  8   9       //  Non-static internal classes can have non-static members  10       Private   Class  Innertest {  11           //  Error non-static internal class cannot have static members 12           //  Private Static int innumber = 2;  13           Private   Int Innumber = 2 ;  14           15           Public  Innertest (){  16 Setnumber (2 );  17 Innumber = innumber + Number; 18 System. Out. println ("innertest ---" + Innumber );  19   }  20   }  21   22       //  Test's private Method  23       Private   Void Setnumber ( Int  Number ){  24          This . Number = Number;  25   }  26   27       //  Constructor  28       Public  Test (){  29 Innertest in = New  Innertest ();  30 System. Out. println ("test" ); 31   }  32   33       Public   Static   Void  Main (string [] ARGs ){  34 Test test = New  Test ();  35           //  Innertest --- 4  36           //  Test 37   }  38 }

Is the first concept well understood .....

 
Two: non-static members of the static internal class can access static variables of the external class, but cannot access non-static variables of the external class.

The relationship between static internal classes and external classes is involved here:

 1   /**  2   *  3    */  4   Package  Com. b510.test;  5   6  Public   Class  Test {  7       Private   Static   Int Number = 1 ;  8       Private String name = "test" ;  9   10       //  Static internal class  11       Private  Static   Class  Innertest {  12           //  The static internal class can have non-static members.  13           Private   Int Innumber = 2 ;  14   15           Public  Innertest (){  16               // The static internal class can access the static members of the external class.  17 Setnumber (2 );  18 Innumber = innumber + Number;  19 System. Out. println ("innertest ---" + Innumber );  20               //  The error static internal class cannot access non-static members of the external class.  21               //  System. Out. println (name );  22  }  23   }  24   25       //  Test static private Method  26       Private   Static   Void Setnumber ( Int  N ){  27 Number = N;  28  }  29   30       //  Constructor  31       Public  Test (){  32 Innertest in = New  Innertest ();  33 System. Out. println ("test" );  34   }  35  36       Public   Static   Void  Main (string [] ARGs ){  37 Test test = New  Test ();  38           //  Innertest --- 4  39           //  Test  40   }  41 }

This is actually very understandable. I don't know what code you think is 15 ~ 23 does not understand ....

 
Three: non-static members of the non-static internal class can access non-static variables of the external class.

This is already mentioned in the first article: 17 lines of one code

 
1Innumber = innumber + number;

Number is a non-static member of an external class. As a member of a non-static internal class, innumber can access the number

Is it easy to understand ....

Summary:

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.