PackageGet_class_method; Public classReflectdemo {/** * @paramargs*/ Public Static voidMain (string[] args) {getClassObject1 (); GetClassObject2 (); GetClassObject3 (); } Public Static voidGetClassObject1 () {person P=NewPerson (); System.out.println (P.getclass (). GetName ()); } /** Mode two: Key Mastery * The bytecode file object of the class can be obtained by the string name of the given class, which is more extensible. * (both the first and the third are not scalable)*/ Public Static voidGetClassObject2 () {Class<Person> Pclass =NULL; Try { /** The name of the class must be written in full (including the package of the Class) * Because the project default path lookup in the bin directory or src directory, if the class name is not complete, you cannot find the class*/Pclass= (class<person>) class.forname ("Get_class_method. Person "); System.out.println (Pclass.getname ()); } Catch(ClassNotFoundException e) {e.printstacktrace (); } } /** Method Three: * Any data type has a static property. class to get its corresponding class of bytecode file object * Relatively simple, but still need to explicitly use static members of the class. is still not enough to expand! */ Public Static voidgetClassObject3 () {Class Pclass= person.class; System.out.println (Pclass.getname ()); } }
Three ways to get the bytecode file object of a class in Java