Java: Inner Class (Static inner class, member inner class, local inner class, anonymous inner Class)

Source: Internet
Author: User

Java Basics _ Inner classes: In Java, a class can be defined inside another class or inside a method, such that a class is called an inner class.

The primary role of inner classes: using internal classes can reduce naming conflicts.

Define location: An inner class can be defined in a class, or in a method, or even in an expression.

Broadly speaking, the inner class is divided into four kinds:

Static inner class (static inner classes)

member Inner class (member inner classes)

Local inner class (local inner classes)

Anonymous inner class (anonymous inner classes)

1, static internal class (static inner classes)

The simplest inner class.

Only static member variables and static methods of external classes can be accessed.

Methods for generating static inner class objects:

Outerclass.innerclass inner = new Outerclass.innerclass ();

related use examples: static inner class

1 classstaticinner{2     Private Static intA =1; 3     /** 4 * Static inner class 5 * Generated class name: Staticinner$innerclass 6*/ 7      Public Static classinnerclass{8//A static inner class can declare a static member variable, and other inner classes cannot9         Private Static intb =1;Ten          Public voidExecute () { One             //Static internal classes can only access static programs ASystem. out. println (A +b); -         } -     } the }//used in the Main method: -  Public Static voidMain (string[] args) { -     //Create a static inner class -Staticinner.innerclass Innerclass =NewStaticinner.innerclass (); +Innerclass.execute (); -}

2. member Inner class (member inner classes):

Member inner classes can access static and non-static methods and member variables of external classes.

To generate a member's inner class object:

Outerclass.innerclass inner = new Outerclass (). New Innerclass ();

Methods for accessing the member variables of an external class in a local inner class:

OUTERCLASS.THIS.A;

Related Use example: member Inner class

1 classmemberinner{2     Private intA =1; 4      Public voidExecute () {5         //Create a member inner class in an external class 6Innerclass Innerclass = This.Newinnerclass ();7     } 9     /**10 * Member Inner class one*/ A      Public classinnerclass{ -         //An inner class can create a member variable with the same name as an external class the         Private intA =2;  -          Public voidExecute () { -             //this refers to the inner class +System. out. println ( This. a); -             //methods for using member variables of external classes inside +System. out. println (Memberinner. This. a); A         } at     } -}//used in the Main method: 1  Public Static voidMain (string[] args) {2     //Create a member inner class3Memberinner.innerclass Innerclass =NewMemberinner ().Newinnerclass ();4Innerclass.execute ();5}

3. Local inner class (local inner classes):

Similar to local variables, cannot be defined as public,protected,private or static types.

In a method, you can only declare a variable of the final type in a method.

Related use example: local inner class

 1 classlocalinner{3      Public voidExecute () {4FinalintA =1; 5      /** 6 Create local inner Class 7*/ 8         classinnerclass{9              Public voidExecute () {TenSystem. out. println ("localinner Class "); A                 //local inner classes can only access variables of the final type -System. out. println (a); -             } the         } -         //can only be created in the method area where -         Newinnerclass (). Execute (); -     } +}//used in the Main method: 1  Public Static voidMain (string[] args) {2     //external cannot directly create local inner class3Localinner Localinner =NewLocalinner ();4Localinner.execute ();5}

4. Anonymous inner class (anonymous inner classes):

Anonymous inner classes do not have class names, and there are no keyword adornments such as the Class keyword or extends and implements.

An anonymous inner class implicitly inherits a parent class or implements an interface.

Related Use example: Anonymous inner class

 1  Public Static voidMain (string[] args) {2Anonymousinnerclasstest test =Newanonymousinnerclasstest ();4     /** 5 * Create anonymous inner Class 6 * Generated class name: Anonymousinnerclasstest$1 7*/ 8Test.print (NewDate () {9         //overriding the ToString () methodTen@Override One          PublicString toString () { A             return "Hello World."; -         } -     }); -JButton button =NewJButton (); -     /**19 * Create anonymous inner class 20 * This anonymous inner class implements ActionListener interface 21 * Generated class name: anonymousinnerclasstest$122*/ atButton.addactionlistener (NewActionListener () { -         //implementing methods in an interface -@Override -          Public voidactionperformed (ActionEvent e) { -System. out. println ("implementing the ActionListener interface using anonymous inner classes"); -         } in     }); - } +  Public voidPrint (date date) { -System. out. println (date); the}

Java: Inner Class (Static inner class, member inner class, local inner class, anonymous inner Class)

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.