Internal class access access to other member restrictions

Source: Internet
Author: User

It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.

In two different situations:

An internal class as a member of a class

1. If the inner class is static

The inner class accesses members of the outer class: only members of the external static identity can be accessed.

The inner class method accesses members of the Inner class: Unrestricted.

2. If the inner class is non-static

There is no limit. Only variables defined within the inner class can no longer be modified with static, which defines int i = 2 within the static method, but it is not possible to define static int i = 2 in the methods approach.


Two, the method defines the inner class

The inner class's ability to access the members of an external class depends on whether the method is static and, if it is static, access only to static variables in the outer class, or, if not static, accessible.

The inner class accesses the members defined in the method: Only members of the final identity in the method can be accessed.

Code:


Package edu.text;

public class Outer {
private int int1 = 1;
private static int int2 = 2;
Private final int int3 = 3;

public void Say () {
String str1 = "str1";
Final String str2 = "str2";
Class inner2{
String STR3 = "STR3";
Final String STR4 = "STR4";
void Speakchinese () {
System.out.println (INT1);
System.out.println (Int2);
System.out.println (INT3);
System.out.println (STR1);//error
System.out.println (STR2);
System.out.println (STR3);
System.out.println (STR4);
}
}

}
Class inner1{
private int int4 = 4;
private static int int5 = 5;//error
Private final int int6 = 6;
public void Eat () {
System.out.println (INT1);
System.out.println (Int2);
System.out.println (INT3);
System.out.println (INT4);
System.out.println (INT6);
}

}

}

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.