Java internal class Things _ let you look at it and figure it out _java

Source: Internet
Author: User
Tags static class

In the "in-depth analysis of Java internals" You can learn something about the Java inner class, but there are some places in the inner class that deserve our careful study ...

Here are some of the things that I've summed up in the Java inner class that I've shared with you guys ....
One: Static inner classes can have static members, but not static inner classes cannot have static members
How do you understand this?
Look at the following code:

Copy Code code as follows:

/**
*
*/
Package com.b510.test;

public class Test {
private int number = 1;

Non-static inner classes can have non-static members
Private class Innertest {
Error non-static inner 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);
}
}

Private method of Test
private void Setnumber (int number) {
This.number = number;
}

Constructors
Public Test () {
Innertest in = new Innertest ();
SYSTEM.OUT.PRINTLN ("test");
}

public static void Main (string[] args) {
Test test = new test ();
Innertest---4
Test
}
}

The first concept is not very well understood ....
Two: non-static members of static inner classes can access static variables of external classes, and non-static variables of external classes are inaccessible
This involves the relationship between the static internal class and the external class:
Copy Code code as follows:

/**
*
*/
Package com.b510.test;

public class Test {
private static int number = 1;
Private String name = "Test";

Static Inner class
private Static Class Innertest {
Static inner classes can have non-static members
private int innumber = 2;

Public Innertest () {
Static inner classes can access static members of external classes
Setnumber (2);
Innumber = innumber + number;
System.out.println ("innertest---" + innumber);
Error static inner class cannot access non-static members of external classes
SYSTEM.OUT.PRINTLN (name);
}
}

Static Private method of test
private static void Setnumber (int n) {
Number = n;
}

Constructors
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 good understanding, do not know you look code 15~23 have no understanding ....
Three: non-static members of non-static inner classes can access non-static variables of external classes
This is already mentioned in the first: 17 lines in one code
Copy Code code as follows:

1 Innumber = innumber + number;

Number is a non-static member of the outer class, and Innumber can access the number as a member of a non-static inner class

Is it well understood ....

To sum up:

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.