Three ways to get the bytecode file object of a class in Java

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.