1, get the type class
We know that everything in Java is an object, and the objects we generally use inherit directly or indirectly from the Object class. The Object class contains a method named getClass, which can be used to obtain the type class of an instance . A type class refers to a class that represents a type, because everything is an object, and types are no exception. In Java, a type class is used to represent a type. All type classes are instances of the Class class. For example, there is the following piece of code:
- A a = new A();
- if(a.getClass()==A.class) {
- System.out.println("equal");
- } else {
- System.out.println("unequal");
- }
- output equal;
- A a = new A();
- if(a.getClass()==B.class) {
- System.out.println("equal");
- } else {
- System.out.println("unequal");
- }
- output unequal;
So, if you know an instance, then you can get the object's type class through the instance's "getClass()" method, and if you know a type, then you can use the ".class" method to get the type class of that type.
getName():String: Get the full name of the type.