Java internal class

Source: Internet
Author: User

I. Internal access rules:

1. The internal class can directly access members in the external class, including private. The reason for direct access to Members in the external class is that the internal class holds an external class reference in the format of: External class name. This;

2. To access internal classes, external classes must create internal class objects;

3. When the internal class defines the member location of the external class and is not private, you can directly create an internal Class Object in other external classes.

Format: External class name. Internal class name variable name = External Class Object. Internal class object;

Outer. Inner in = new outer (). New inner ();

Public class innerdemo {public static void main (string [] ARGs) {// outer out = new outer (); // out. method (); // directly access the internal class Outer outside the external class. inner in = new outer (). new inner (); In. fun (); // system. out. println (in. x); Compilation error X is private and can only be accessed in this class} class Outer // external class {private int x = 3; // Private member variable of the external class // Private class inner // internal class, which exists as a member of the external class. You can use private to modify {private int x = 4; void fun () {system. out. println (outer. this. x); // output 3system. out. println (This. x); // output 4 show (); // access the show method of the external class} void method () {// The external class wants to access the members and functions of the Internal class. The instance object inner in = new inner () of the internal class should be created; // create the instance system of the internal class. out. println (in. x); In. fun ();} private void show () {system. out. println ("external class show Method !!! ");}}

Ii. static internal class

When the internal class is in the member position, it can be modified by the member modifier. For example, private: encapsulate internal classes in external classes.

Static: Internal classes have the static feature. After the internal class is modified as static, you can only directly access static members in the external class, and access restrictions are imposed.

Public class innerdemo01 {public static void main (string [] ARGs) {outer. inner in = new outer. inner (); In. fun (); // access internal class non-static method format: new external class name. internal Class Name (). (variable or method name); outer. inner2.show (); // access the static method format in the internal class: External class name. internal class name. (variable or method name) ;}} class Outer {Private Static int x = 3; static class inner // static internal class {void fun () {system. out. println ("inner fun:" + x); // The static internal class can only access static in the external class, cannot access non-static} static class inner2 // define another static internal class {static void show () {system. out. println ("inner2 show ");}}}

Iii. Local internal class

When the internal class is defined locally:

1. cannot be modified by the member Modifier

2. You can directly access the members of the External class because they still hold references to the external class, but cannot access the variables in the local part where it is located. Only local variables modified by final can be accessed.

Public class innerdemo02 {public static void main (string [] ARGs) {New outer (). method (6) ;}} class Outer {private int x = 3; // void method (final int A), an external class member variable) // final local variable A {final int y = 4; // final local variable yclass inner // defines a local internal class {void fun () {system. out. println (x); system. out. println (y); system. out. println (a) ;}} new inner (). fun (); // create an internal class instance to access the internal class method }}

Iv. Anonymous internal class

1. The anonymous internal class is in short format.
2. premise for defining anonymous internal classes: Internal classes must inherit a class or implement interfaces.
3. Format of the anonymous internal class: new parent class or interface () {defines the content of the subclass}
4. In fact, an anonymous internal class is an anonymous subclass object. This object is a bit fat and can be understood as an object with content.
5. It is recommended that no more than three methods are defined in the anonymous internal class.

Public class innerdemo03 {public static void main (string [] ARGs) {New outer (). method () ;}} abstract class person {abstract void Study ();} class Outer {private int num = 3; class student extends person // internal class {void Study () // override the study method of the parent class {system. out. println ("Learning Java");} void play () // subclass add method {system. out. println (Num); system. out. println ("playing basketball") ;}} public void method () {student s = new student (); S. study (); S. play ();}}

Public class innerdemo04 {public static void main (string [] ARGs) {New outer (). method () ;}} abstract class person {abstract void Study ();} class Outer {private int num = 3; Public void method () {/* person P = new person () {void Study () {system. out. println ("Learning Java");} void play () {system. out. println (Num); system. out. println ("playing basketball") ;}}; p. study (); // P. play (); Compilation failed * // anonymous internal class new person () {void Study () {system. out. println ("Learning Java ");}}. study ();}}

Internal exercises

Public class innertest {public static void main (string [] ARGs) {outer. method (). show (); // complete the code according to this sentence // note the following writing format: new object () {void show () {system. out. println ("show run .... ");}}. show () ;}} interface inter {void show ();} class Outer {static int num = 99; // The method is the completion code public static inter method () {inter in = new Inter () {public void show () {system. out. println ("num =" + num) ;}; return in ;}}

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.