There are 4 forms of Java internal classes we generally use: general internal classes, local internal classes, anonymous internal classes, and static internal classes. The following is a test I made to illustrate the characteristics of various internal classes.
Regarding the characteristics of internal classes, the code is explained in detail, as follows.
/** java internal class test* * InterObj reflection result: * * private int i* private InterObj$InterA ia* public InterObj()* public static void main(java.lang.String[])* private int getI()* public void p()* public void pi()* public void pp()* public static void ppp()* public void pp()* public void ppp()* public void pppp()* below is the compiler automatically generated static methods for accessing private attributes or method package level* static int access$0(InterObj)*/public class InterObj {private int i=8;private InterA ia=null;public InterObj(){ia=new InterA();}private int getI(){return i;}public void p(){pi();pp();ppp();pppp();pppp();}/* * In general internal classes, methods and properties can be accessed at any level in the "coat class". The jacket class can also* access methods and properties at any level in the "internal class". Because the inner class can hold a reference to the coat class object. * For the private methods and attributes that need to be accessed by the internal class, the compiler will automatically generate the "package"-level static methods corresponding to the * private methods and attributes. These methods need to use the external class object as the * parameter, so that you can access the private methods and * attributes in the outer class in the "package"-level static methods. * The same principle is the same for the jacket class to access private methods and properties in the inner class. When the inner class is compiled, * will generate a "package"-level static method corresponding to the private methods and properties that need to be accessed by the jacket class. * * InterA reflection result: * private int ia * Below is the reference of the coat class object held by the internal class* final InterObj this$0 * Use the coat class object as a parameter in the constructor* InterObj$InterA(InterObj) * private void pA() * Below is the compiler automatically generates a static method for accessing private attributes or method package level* static void access$0(InterObj$InterA) * public void pA1() * */class InterA{private int ia=9;private void pA(){System.out.println("this is InterA.pA: ia="+ia+",InterObj.i="+getI());}public void pA1(){System.out.println("this is InterA.pA1: ia="+ia+",InterObj.i="+getI());}}/* * Local internal classes are only visible inside methods, and other features are the same as general internal classes. * For local variables that need to be accessed, it must be set to final, because although the local inner class can hold * references to the outer class object to access properties and methods, it cannot access local variables in the external class method. All compilers "copy" a copy of local variables that need to be accessed in the local inner class (but there is no copy for types whose basic types int, float and String * are not changed). In order to ensure that the copied variable value and the * object pointed to by the value of the variable in the external method are the same object, it is required that those local variables used by the local class should be set to final and cannot be modified *, so as to ensure that the copied variables in the local part and the variables in the external method point to the same object. Setting the variable * to final only controls that the object address pointed to by the variable remains unchanged, rather than that the internal properties of the object it points to cannot be changed. * * Reflection result of InterB: * * private int ib * Below is the reference of the coat class object held by the inner class* final InterObj this$0 * Below is the reference copy of the local variable Test object in the external method held by the inner class* private final Test val$test * Use the coat class object and the local variable Test as parameters in the constructor* InterObj$1$InterB(InterObj,Test) * private void pB() * Below is the compiler automatically generates a static method for accessing private properties or method package level* static void access$0(InterObj$1$InterB) */public void pi(){final int s=5;final Test test=new Test();class InterB{private int ib=7;private void pB(){System.out.println("this is InterB.pB: ib="+ib+ ",(Method)pi.s="+s+",Test.t="+test.getT());}}InterB ib=new InterB();//This is changed the internal state of the Test test referenced by the local internal class. // When the result is called ib.pB(), the output is the changed value 100test.setT(100);ib.pB();}/* * Static inner class, used when there is no need to hold a reference to the "coat class object". * * InterC reflection result: (The static inner class does not have a reference to the coat class object) * private int ic * InterC() * private void pC() */static class InterC{private int ic=6;private void pC(){System.out.println("this is InterC.pC: ic="+ic);}}/* * Non-static method, you can construct static and non-static inner classes. * You can access any permissions in the inner class*/public void pp(){InterA ia=new InterA();ia.pA();ia.pA1();InterC ic=new InterC();ic.pC();// Local internal classes are only visible inside the method //InterB ib=new InterB();}/* * Static methods can only construct static internal classes. * You cannot construct non-static inner classes because there is no object in the static method that refers to the "coat class" to construct * The inner class object that needs to be referenced by the coat class object. */public static void ppp(){//InterA ia=new InterA();//But it can be constructed as follows: InterObj iobj=new InterObj();InterA ia=iobj.new InterA();ia.pA();ia.pA1();InterC ic=new InterC();ic.pC();// Local internal classes, only visible inside the method //InterB ib=new InterB();}/* * Anonymous internal class test*/public void pppp(){TestInterface tif=new TestInterface(){public void pppp() {System.out.println("TestInterface.noName");}};tif.pppp();}/* * Running result: * this is InterB.pB: ib=7,(Method)pi.s=5,Test.t=100 * this is InterA.pA: ia=9,InterObj.i=8 * this is InterA.pA1: ia=9,InterObj.i=8 * this is InterC.pC: ic=6 * this is InterA.pA: ia=9,InterObj.i=8 * this is InterA.pA1: ia=9,InterObj.i=8 * this is InterC.pC: ic=6 * TestInterface.noName */public static void main(String[] args) {InterObj io=new InterObj();io.p();}}/** Interface for creating internal classes*/interface TestInterface{public void pppp();}/** Local variable class for testing local internal classes*/class Test{private int t=9;public int getT(){return t;}public void setT(int t1){t=t1;}}Let me share another example:
public class InnerClass {static Toy toy = new Toy(){String name = "Lao Wu";@Override public void jump() {System.out.println(name+"Breaking out of the earth");go();}};/*Inner class: Class defined inside the class*1. Member internal class: *1.1 Member internal class can directly access the properties of the external class*1.2 Access the current object of the external class through the external class name. This way * Member internal class instantiated object: External class name. Internal class name reference name = External class object.new internal class name(); *2. Static inner class*2.1 The member resources of the external class cannot be accessed inside the static inner class, and can only access the static resources of the external class through the class name*static inner class instantiation object: External class name. Internal class name reference name = new external class name. Internal class name(); *3. Local inner class: *3.1 You can also directly access the properties of the external class*3.2 You can also access the current object of the external class*3.3 The local inner class can only be accessed inside the method, and the modifier can only be the default*4. Anonymous inner class: When a specific subclass instance of a class is needed, a class is temporarily generated using *new class name(){ * overwrite method; *}; *4.1 Anonymous inner class accesses the properties of the external method, and this property will be converted into a constant*4.2 New attributes and methods added in the anonymous inner class, and can only be used inside the anonymous inner class**/public static void main(String[] args) {Person per = new Person("Lao Chen",18);Person.Computer pc = per.new Computer("Alien");Person.Computer pc1 = new Person("Jian Zihao",18).new Computer("Alien");pc.runGame();pc1.runGame();Person.Computer1 pc11 = new Person.Computer1("Internet cafe computer");pc11.runGame();per.useComputer();String str = "Lalala";//str = "Rokudou";Computer com = new Computer(){@Override public void runGame() {// TODO Auto-generated method stubSystem.out.println(per.age+"year-old "+per.name+" playing Lalalala demasita");System.out.println(str);}};com.runGame();//Anonymous internal class exclusive for specific classes/*Toy toy = new Toy(){ @Override public void jump() { System.out.println("Breaking out of the earth"); } };*/toy.jump();toy.jump();//toy.go();//System.out.println(toy.);}}class Person {String name;int age;static int age1 = 18;static String name1 = "Full-time expert";public Person(String name,int age) {super();this.name = name;this.age = age;}public void playGame(){System.out.println(name+"play game");}public class Computer{String name;public Computer(String name) {super();this.name = name;}public void runGame(){System.out.println(name+"run game");System.out.println(age+"year-old"+Person.this.name+"play game");}}public static class Computer1{String name;public Computer1(String name) {super();this.name = name;}public void runGame(){System.out.println(name+"run game");System.out.println(Person.age1+""+Person.name1+"playing game");}}public void useComputer(){class Computer{String name;public Computer(String name) {super();this.name = name;}public void runGame(){System.out.println(name+"run game");System.out.println(Person.this.age+""+Person.this.name+"playing game");}}Computer com = new Computer("Notebook");com.runGame();}}public interface Computer {void runGame();}public class Toy {public void jump(){System.out.println("Jump toy");}}Summarize
The above is all the detailed explanation of Java internal test code in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!