Four internal classes are created in java, three methods, and internal data access permissions.

Source: Internet
Author: User

Four internal classes are created in java, three methods, and internal data access permissions.

The relationship between the internal class and the external class, the data access permission of the internal static class, and the declaration method.

First, the internal class is declared to use the internal class. The internal class type is static and non-static, and the internal class data type is private, protected, and public. The external class is non-static and has the following permissions:

Package com; public class Test1 {// @ author Zhang chunlei private class Name {private int I = 1; public int s = 2; protected int m = 3 ;} public static void main (String [] args) {Test1 test = new Test1 (); Test1.Name name = test. new Name (); System. out. print (name. i); System. out. print (name. s); System. out. print (name. m );}}
Declaration method, and all internal parameters can be output regardless of anything. When the internal class is a static class, the declaration method changes:

Package com; public class Test1 {// @ author Zhang chunlei private static class Name {private int I = 1; public int s = 2; protected int m = 3 ;} public static void main (String [] args) {Test1 test = new Test1 (); Test1.Name name = new Name (); System. out. print (name. i); System. out. print (name. s); System. out. print (name. m );}}
The above method is used for access within Test1. Currently, the external Test2 force access is the second type. When the internal class is non-static, the declaration method is as follows:

Package com; public class Test1 {// @ author Zhang chunlei protected class Name {private int I = 1; public int s = 2; protected int m = 3 ;} public static void main (String [] args) {Test2 t = new Test2 (); t. test () ;}} class Test2 {public void test () {Test1 test = new Test1 (); Test1.Name name = test. new Name (); // same as the first case: System. out. println (name. m); System. out. print (name. s );}}

 

The method is the same as that of the first non-static internal class, but the internal static variables cannot be accessed. When the internal class is static, the access mode is as follows: 

Package com; public class Test1 {// @ author Spring Bud protected static class Name {private int I = 1; public int s = 2; protected int m = 3 ;} public static void main (String [] args) {Test2 test2 = new Test2 (); test2.test () ;}} class Test2 {public void test () {Test1.Name name2 = new Test1.Name (); System. out. print (name2.m); System. out. print (name2.s );}}






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.