About Inner classes:
Effect: 1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the Class 2. The methods of the inner class can access all the data of the external class directly, including the private data 3. Functions implemented by inner classes can also be implemented using external classes, but sometimes it is easier to use internal classes
Category: member inner class static inner class method inner class (i.e. inner class defined in method of external Class) anonymous inner class
For example, to invoke a generic inner class, you must first instantiate the
intA=1; intb=3; //Inner class inner, class inner inside class HelloWorld Public classInner {intA=2; //methods for inner classes Public voidShow () {System. out. println ("Welcome!"+a+b+outer. This. a); //Use this to use the member variable of the external class when there is a name inside and outside } } Public Static voidMain (string[] args) {//creating an external class objectHelloWorld Hello =NewHelloWorld ();//after defining the member inner class,//you must use an external class object to create an inner class object instead of going directly to the new//an inner class object, that is, an inner class object name = External class object. New Inner class ()//creating an inner class objectInner i = hello.NewInner (); //The method that calls the inner class object cannot directly use the method of the inner class.i.show (); }}
In the General class:
A static member (method or property) can be accessed directly by using the class name or by using the object name.
A static method can directly invoke a static member in the same class, but cannot call a non-static member directly (which must be called through an object).
In the normal member method, you can directly access the same non-static variables and static variables
Inside the class:
1 if the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name
2 static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external class (). Member
3 When you create an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner class (); External class
//External Class Public classHelloWorld {//static variables in the outer class score Private Static intScore = -; //non-static variables for external classes Private intA=1; //Create a static inner class Public Static classSinner {//variables in the inner class score intScore = the; Public voidShow () {System. out. println ("to access score in an external class:"+Helloworld.score); System. out. println ("to access the score in the inner class:"+score); System. out. println ("accessing the External Class A:"+NewHelloWorld (). a); } } //testing static Internal classes Public Static voidMain (string[] args) {System. out. println ("to access score in an external class:"+ score+"accessing external class non-static a"+NewHelloWorld (). a); //Static members of the class can be called directly in a static method, but non-static members cannot be called//directly create an object of the inner classSinner si=NewSinner (); //call the Show methodsi.show (); }}
Java's easy-to-confuse internal class-related concepts