Detailed explanation of Java internal classes
Inner class
Definition of internal class
Defining a class in another given class or method is called an inner class.
Internal classes can be divided into four types: member internal classes, local internal classes, anonymous internal classes and static internal classes. Let's introduce these four internal classes one by one.
Member inner class
It is defined in another class. The general definition format is as follows
class C{ class D{ } }
Because class C is outside relative to class D, we also call class c an external class.
The internal class of a member can unconditionally access the properties and methods of the external class, but when the external class wants to access the properties or methods of the internal class, it must create an internal class object, and then access the properties or methods of the internal class through the object
The inner class of a member unconditionally accesses the properties and methods of the outer class
class C{ private String name = "External class"; public void run(){ System.out.println("External class running"); } class D{ public void say(){ System.out.println(name); run(); } } }
External classes access internal class properties and methods
class C{ private String name = "External class"; public void run(){ System.out.println("External class running"); } /*Use the properties and methods of the inner class*/ public void eat(){ D d = new D(); System.out.println(d.value); d.say(); } class D{ private String value = "DDD"; public void say(){ System.out.println(name); run(); } } }
Hide external class properties or methods
If the attribute or method of the internal class of a member has the same name as the external class, these attributes and methods of the external class will be hidden in the internal class. You can also call the external class according to this format this. Properties / methods.
class C{ private String name = "External class"; public void run(){ System.out.println("External class running"); } /*Use the properties and methods of the inner class*/ public void eat(){ D d = new D(); System.out.println(d.value); d.say(); } class D{ private String value = "DDD"; private String name = "Inner class"; public void say(){ System.out.println(C.this.name); System.out.println(name); run(); } } }
Create internal class object
Obviously, the internal class of a member is parasitic on the external class. To create an internal class object, you must first create an external class object. There are two ways to create inner classes later.
public class Test10 { public static void main(String[] args) { /*Method 1: create internal class objects of members*/ C c = new C(); C.D d = c.new D(); /*Method 2: create internal class objects of members*/ C.D d1 = c.getClassD(); } } class C{ private String name = "External class"; public void run(){ System.out.println("External class running"); } /*Create a method that returns the D object*/ public D getClassD(){ return new D(); } /*Use the properties and methods of the inner class*/ public void eat(){ D d = new D(); System.out.println(d.value); d.say(); } class D{ private String value = "DDD"; private String name = "Inner class"; public void say(){ System.out.println(C.this.name); System.out.println(name); run(); } } }
Access rights of internal classes of members
Four access modifiers can be added before the inner class of a member.
private: only external classes can access.
protected: accessible under the same package or inherited classes.
default: accessible under the same package.
public: all classes are accessible.
Local inner class
Exists inside the class.
The difference between it and member inner classes is that the access rights of local inner classes are limited to methods or scopes.
class K{ public void say(){ class J{ } } }
Note: local internal classes are like local variables. Modifiers and static modifiers cannot be accessed in front of them.
Anonymous Inner Class
Let's take a preliminary look at the anonymous inner class through a piece of code.
public class Test13 { public static void main(String[] args) { driveCar(new Car(){ @Override public void drive() { System.out.println("Driving BMW automobile"); } }); } public static void driveCar(Car car){ car.drive(); } } interface Car { void drive(); }
After analyzing the above code, we know that the static method driveCar needs a Car object. We create an anonymous class object through the implementation interface and pass it to the past. In fact, you can also create an anonymous inner class object by inheriting a class.
Note: anonymous inner classes have no constructor. It is also the only inner class without a constructor. Anonymous inner classes and local inner classes can only access the final variable of the external class.
Static inner class
Static inner class has one more static modifier than member inner class. It is similar to the static member variable of a class and does not depend on the external class. At the same time, the static inner class also has its particularity. Because only static fields will be loaded when external classes are loaded, static internal classes cannot use non static variables and methods of external classes.
At the same time, you can know that the inner class of a member cannot contain static properties or methods.
class U { static class I { } }
Benefits of inner classes
- The Java multi inheritance mechanism is improved. Since each internal class can inherit an interface or class independently, whether the external class inherits or implements a class or interface has no impact on the internal class.
- Easy to write event driver.
summary
public class Test15 { public static void main(String[] args) { //Initialize bean1 Test15.Bean1 bean1 = new Test15().new Bean1(); bean1.i++; //Initialize bean2 Test15.Bean2 bean2 = new Test15.Bean2(); bean2.j++; //Initialization 3 Bean bean = new Bean(); Bean.Bean3 bean3 = bean.new Bean3(); bean3.k++; } class Bean1 { public int i = 0; } static class Bean2 { public int j = 0; } } class Bean { class Bean3 { public int k = 0; } }
The creation of static internal class objects is generally external classes Internal class name = new external class Internal class ();
The creation of internal class objects of members is generally external class Internal class name = external class object name new inner class ();