the inner classes in Java are divided into four types :
Static inner class static inner class (also called nested Class)
member Inner class member inner class
Local internal class local inner class
Anonymous inner class anonymous inner class
static inner class static Inner class
The simplest form of inner class.
The static keyword is added to the class definition .
Cannot have the same name as an external class.
is compiled into a completely separate. class file with the name Outerclass$innerclass.class in the form.
only static members and static methods of external classes can be accessed , including private static members and methods.
The static inner class object is generated in the following way:
Outerclass.innerclass inner = new Outerclass.innerclass ();
Static internal classes use code:
PackageCom.learnjava.innerclass;classstaticinner{Private Static intA = 4; //static inner class Public Static classInner { Public voidTest () {//static inner classes can access static members of external classes//and it can only access the staticSystem.out.println (a); } }} Public classstaticinnerclasstest{ Public Static voidMain (string[] args) {Staticinner.inner Inner=NewStaticinner.inner (); Inner.test (); }}
static internal class use Test
member Inner class member Inner class
The member inner class is also defined in another class, but is not defined with a static adornment.
A member inner class and a static inner class can be likened to non-static member variables and static member variables.
The member inner class is like an instance variable.
It can access all the member variables and methods of its outer class, whether static or non-static .
Create an instance of the member's inner class inside the outer class:
This.new Innerclass ();
Create an instance of an inner class outside the outer class:
(New Outerclass ()). New Innerclass ();
To access members of an external class in an inner class:
Outerclass.this.member
See the code example for details:
PackageCom.learnjava.innerclass;classmemberinner{Private intD = 1; Private intA = 2; //define a member inner class Public classInner2 {Private intA = 8; Public voiddosomething () {//Direct access to external class objectsSystem.out.println (d); System.out.println (a);//direct access to a, you are accessing a in the inner class//How to access a in an external class? System.out.println (Memberinner. This. a); } }} Public classmemberinnerclasstest{ Public Static voidMain (string[] args) {//create an object of a member's inner class//you need to create an instance of an external class firstMemberinner.inner2 inner =NewMemberinner ().NewInner2 (); Inner.dosomething (); }}
member internal class use Test
local internal class local Inner class
Local inner classes are defined in a method and are smaller than the range of methods. Is the least used type in an inner class.
Like local variables, it cannot be modified by public, protected, private, and static.
Only local variables of the final type defined in the method can be accessed.
Local inner classes are defined in methods, so they can only be used in methods, that is, you can only generate instances of local inner classes within a method and invoke their methods.
PackageCom.learnjava.innerclass;classlocalinner{intA = 1; Public voiddosomething () {intb = 2; Final intc = 3; //define a local inner class classInner3 { Public voidTest () {System.out.println ("Hello World"); System.out.println (a); //non-final local variables cannot be accessed//Error:cannot refer to a non-final variable B inside an inner//class defined in a different method//System.out.println (b); //can access final variablesSystem.out.println (c); } } //create an instance of a local inner class and invoke a method NewInner3 (). Test (); }} Public classlocalinnerclasstest{ Public Static voidMain (string[] args) {//creating an external class objectLocalinner inner =NewLocalinner (); //methods for calling external classesinner.dosomething (); }}
local inner class use Test
anonymous inner class anonymous Inner class
The anonymous inner class is a local inner class without a name, not using the keyword class, extends, implements, and no constructor method.
An anonymous inner class implicitly inherits a parent class or implements an interface .
Anonymous inner classes are used more often as a method parameter.
In the generated. class file, the anonymous class generates the Outerclass$1.class file, which is based on the first few anonymous classes.
PackageCom.learnjava.innerclass;Importjava.util.Date; Public classanonymouseinnerclass{@SuppressWarnings ("Deprecation") PublicString getDate (date date) {returndate.tolocalestring (); } Public Static voidMain (string[] args) {anonymouseinnerclass test=NewAnonymouseinnerclass (); //Print Date:String str = test.getdate (NewDate ()); System.out.println (str); System.out.println ("----------------"); //using anonymous inner classesString str2 = Test.getdate (NewDate () {});//curly braces are used, but the content is not filled in, and the result is exactly the same as above//an object that inherits a subclass of the date class was generatedSystem.out.println (STR2); System.out.println ("----------------"); //use anonymous inner class, and override methods in parent classString STR3 = Test.getdate (NewDate () {//overriding methods in a parent class@Override @Deprecated PublicString tolocalestring () {return"Hello:" +Super. tolocalestring (); } }); System.out.println (STR3); }
Anonymous internal class usage test
Java-based partial class