Brief description:
Since I have encountered many classes that need reflection recently, and there are many internal classes among them. Here I will summarize the rules of fully qualified names of internal classes.
Member internal class
The test results show that regardless of whether the internal class inside the member is static or not, its fully qualified name is used with the following naming method:
Package name.External class name$Internal class name
Test code:
package com.test;public class InnerClassTest {static class StaticInner{}class Inner{}public static void main(String[] args) {StaticInner si = new StaticInner();Inner in = new InnerClassTest().new Inner();System.out.println(si.getClass());System.out.println(in.getClass());}}Print result:
classcom.test.InnerClassTest$StaticInner
classcom.test.InnerClassTest$Inner
Anonymous internal class
The fully qualified names of anonymous internal classes meet the following rules:
Package name. Positive integer starting with external class name $1 - arranged in order of class loading
Test code
package com.test;public class InnerClassTest {static Object staticAnClass = new Object(){};static Object staticBlockAnClass;static {staticBlockAnClass = new Object(){};}Object anClass = new Object(){};Object blockAnClass;{blockAnClass = new Object(){};}public static void main(String[] args) {InnerClassTest functionClass = new InnerClassTest(){};System.out.println(staticAnClass.getClass());System.out.println(staticBlockAnClass.getClass());InnerClassTest test = new InnerClassTest();System.out.println(test.anClass.getClass());System.out.println(test.blockAnClass.getClass());System.out.println(functionClass.getClass());}} Print result:
class com.test.InnerClassTest$1
class com.test.InnerClassTest$2
class com.test.InnerClassTest$3
class com.test.InnerClassTest$4
class com.test.InnerClassTest$5
Local internal classes
The fully qualified names of local internal classes are used as follows:
Package name. External class name $ is followed by a positive integer starting with 1 followed by a local class name - where the numeric part is the order in which the local class appears in the context of the external class
Test code
package com.test;public class InnerClassTest {public static void main(String[] args) {functionA();functionB();}public static void functionA() {class Inner {};System.out.println(new Inner().getClass());}public static void functionB() {class Inner {};System.out.println(new Inner().getClass());}} The above example prints the result:
classcom.test.InnerClassTest$1Inner
classcom.test.InnerClassTest$2Inner
Based on the test code, the result after exchanging the functionA and B positions in main:
classcom.test.InnerClassTest$2Inner
classcom.test.InnerClassTest$1Inner
The result of exchanging the locations of functionA and B declarations based on the test code:
classcom.test.InnerClassTest$2Inner
classcom.test.InnerClassTest$1Inner
Summarize
The above is the entire content of this article about the fully qualified name rule code example of Java internal classes. 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!