are divided into four types: member inner classes, local inner classes, static inner classes and anonymous inner classes.
1. member inner class: it exists as a member of the external class, and is juxtaposed with the properties and methods of the external class.
note: static variables cannot be defined in the inner class of members, but all members of the outer class can be accessed.
public class Outer{
private static int i = 1;
private int j=1;
private int k=2;
public static void outer _ f1 () {
/do more something
}
public void outer _ F2 () {
//do more something
}
//member inner class
classinner {
/. //Static variable
int j=1 is not allowed to be defined in the inner class; //The instance variable of the external class in the internal class can be * * * saved as
int inner_i=1;
void inner_f1(){
System.out.println(i); //If the variable of the external class does not have the same name as the variable of the internal class, you can directly access the variable of the external class
System.out.println(j) with the variable name; //To access the inner class's own variables in the inner class, directly use the variable name
System.out.println(this.j); //You can also use "this. variable name" in the inner class to access the inner class variable
// You can use "external class name. this. variable name" to access the instance variable with the same name in the outer class.
System.out.println(k); //If the variable of the external class does not have the same name as the variable of the internal class, you can directly access the variable of the external class with the variable name
outer_f1 ();
outer_f2();
}
}
// Non-static method of external class accesses member internal class
public void outer _ F3 () {
inner inner = new inner ();
inner.inner_f1();
}
// The static method of the external class accesses the internal class of the member, just like accessing the internal class of the member outside the external class.
Public static void outer _ F4 () {
//Step 1 establishes the external class object
Outer out = new Outer ();
/* * * Step 2 Establish an internal class object according to the external class object ***
Inner inner=out.new Inner ();
//step3 method of accessing inner class
inner.inner_f1 ();
}
public static void main(String[] args){
outer_f4();
}
}
Advantages of the member inner class:
(1) As a member of the external class, the inner class can access the private members or properties of the external class. (Even if the external class is declared as PRIVATE, it is still visible to the internal class inside it. )
(2) Use the inner class to define the properties that are inaccessible in the outer class. In this way, the access right in the external class is smaller than the private of the external class.
Note: Inner classes are a concept at compile time. Once compiled successfully, they will become two completely different classes. For an external class named outer and its internally defined internal class named inner. After compilation, there are two categories: outer.class and outer$inner.class
2. Local inner class: that is, an inner class defined in a method, similar to a local variable, with no modifier public or private before it, and its scope is the code block that defines it.
note: static variables cannot be defined in local inner classes, and local variables of external classes (that is, variables within methods) can be accessed, but the variables must be final.
public class Outer {
private int s = 1;
private int out_i = 1;
public void f(final int k){
final int s = 2;
int i = 1;
final int j = 1;
class Inner{ // is defined inside the method
int s = 3; //you can define a variable with the same name as an external class
//static int m = 2; //Static variable
Inner(int k){
inner_f(k) cannot be defined;
}
int inner_i = 1;
void inner_f(int k){
System.out.println(out_i); //If the inner class has no variable with the same name as the outer class, the instance variable
System.out.println(k) of the outer class can be directly accessed in the inner class; //* * * * can access local variables of external classes (that is, variables within methods), but the variables must be * * * *
//system.out.println (i) of final;
System.out.println(s); //If there is a variable with the same name as the external class in the internal class, the variable
System.out.println(this.s) of the internal class is directly accessed with the variable name; //the internal class variable
system. out.println (outer.this.s) is accessed with "this. variable name"; //The external "external class name. this. variable name" is used to access the external class variable
}
new inner (k);
}
public static void main (string [] args) {
//Access to a local internal class must have an external class object first
Outer out = new Outer ();
out.f(3);
}
}
Note:
Local internal classes cannot be generated directly outside the class (to ensure that the local internal classes are invisible to the outside world). If you want to use a local inner class, you need to generate an object, and the object calls the method, and then you can call its local inner class in the method. A forced weak coupling is achieved between the inner class and the interface, and the interface is realized by the local inner class, and the interface type is returned in the method, so that the local inner class is invisible and the visibility of the implementation class is shielded.
3. static inner class: the static inner class is defined in the class, except for any method, it is defined by static.
note: static or non-static members can be defined in static inner classes
public class outer {
private static int i = 1;
private int j = 1;
public static void outer _ f1 () {
}
public void outer _ F2 () {
}
// Static inner classes can be decorated with public,protected,private private
//Static inner classes can define static or non-static members <
int inner_j = 2;
static void inner_f1(){
System.out.println("Outer.i"+i); //The static inner class can only access the static member
outer_f1 () of the outer class; //including static variables and static methods
}
void inner _ F2 () {
//system. out.println ("outer.i"+j); //Static inner classes cannot access non-static members of outer classes
// outer_f2 (); //including non-static variables and non-static methods
}
}
public void outer _ F3 () {
//The external class accesses the static member of the internal class: internal class. static member
system. out.println (inner.inner _ i);
Inner.inner_f1();
// The external class accesses the non-static members of the internal class: just instantiate the internal class
Inner inner = new Inner ();
inner.inner_f2();
}
public static void main(String[] args) {
new Outer().outer_f3();
}
}
Note: * * * * * Generating (new) a static inner class does not need external class members: this is the difference between a static inner class and a member inner class. Objects of static inner classes can be generated directly:
outer.inner = new outer.inner ();
without generating external class objects. This actually makes the static inner class a top-level class. Static inner classes cannot be defined by private. * * * * * *
Example:
For two classes, have the same method:
class People
{
run ();
}
class Machine{
run();
}
At this time, there is a robot class:
Class Robot Extensions People Implementation Machine.
At this time, run () cannot be directly implemented.
note: when there is a method naming conflict between a class and an interface (or an interface and an interface), it must be implemented by using an inner class. Multi-inheritance can't be realized completely with interfaces, but with interfaces and internal classes, real multi-inheritance can be realized.
4. Anonymous inner class
Anonymous inner class is a special local inner class, which implements the interface through anonymous classes.
IA is defined as an interface.
IA I=new IA(){};
characteristics of anonymous inner classes:
1. A class is used to inherit other classes or implement interfaces, and it does not need to add additional methods, but only to pre-or override the inherited methods.
2, just to get an object instance, you don't need to know its actual type.
3, the class name is meaningless, that is, it is not needed.
public class Outer {
private static int i = 1;
private int j = 1;
public static void outer _ f1 () {
}
public void outer _ F2 () {
}
// Static inner classes can be decorated with public,protected,private private
//Static inner classes can define static or non-static members <
int inner_j = 2;
static void inner_f1(){
System.out.println("Outer.i"+i); //The static inner class can only access the static member
outer_f1 () of the outer class; //including static variables and static methods
}
void inner _ F2 () {
//system. out.println ("outer.i"+j); //Static inner classes cannot access non-static members of outer classes
// outer_f2 (); //including non-static variables and non-static methods
}
public void outer _ F3 () {
//External classes access static members of internal classes: internal classes. static members
system. out.println (inner.inner _ i);
Inner.inner_f1();
// The external class accesses the non-static members of the internal class: just instantiate the internal class
Inner inner = new Inner ();
inner.inner_f2();
}
public static void main(String[] args) {
new Outer().outer_