Java internal class things _ let you understand at a glance

Source: Internet
Author: User

In the "in-depth analysis based on the internal functions of java classes", you can understand some of the internal classes of java, 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 following code:

Copy codeThe Code is as follows :/**
*
*/
Package com. b510.test;

Public class Test {
Private int number = 1;

// Non-static internal classes can have non-static members
Private class InnerTest {
// Error non-static internal class cannot have static members
// Private static int inNumber = 2;
Private int inNumber = 2;

Public InnerTest (){
SetNumber (2 );
InNumber = inNumber + number;
System. out. println ("innerTest ---" + inNumber );
}
}

// Test private Method
Private void setNumber (int number ){
This. number = number;
}

// Constructor
Public Test (){
InnerTest in = new InnerTest ();
System. out. println ("test ");
}

Public static void main (String [] args ){
Test test = new Test ();
// InnerTest --- 4
// Test
}
}

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:Copy codeThe Code is as follows :/**
*
*/
Package com. b510.test;

Public class Test {
Private static int number = 1;
Private String name = "test ";

// Static internal class
Private static class InnerTest {
// The static internal class can have non-static members
Private int inNumber = 2;

Public InnerTest (){
// The static internal class can access the static members of the external class
SetNumber (2 );
InNumber = inNumber + number;
System. out. println ("innerTest ---" + inNumber );
// Error static internal class cannot access non-static members of the external class
// System. out. println (name );
}
}

// Test static private Method
Private static void setNumber (int n ){
Number = n;
}

// Constructor
Public Test (){
InnerTest in = new InnerTest ();
System. out. println ("test ");
}

Public static void main (String [] args ){
Test test = new Test ();
// InnerTest --- 4
// Test
}
}

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 codeCopy codeThe Code is as follows: 1 inNumber = 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.