javase-Reflection-Four ways to get classes or objects

Source: Internet
Author: User

1, the use of class static method Class.forName ("xxxx");

Create a new class that you want to get

 Package Org.burning.sport.javase.classloader;  Public class Classloadermodel {}
 PackageOrg.burning.sport.javase.classloader;/*** Class loading mode*/ Public classClassloadermain { Public Static voidMain (string[] args) {classloadermain CLM=NewClassloadermain (); //1. Using the class static method Class.forNameclm.loadclassmethod1 (); }     Public voidloadClassMethod1 () {Try{Class<?> clazz = Class.forName ("Org.burning.sport.javase.classloader.ClassLoaderModel"); Classloadermodel CLM=(Classloadermodel) clazz.newinstance ();        System.out.println (CLM); } Catch(Exception e) {e.printstacktrace (); }    }}
2. Using ClassLoader
 PackageOrg.burning.sport.javase.classloader;/*** Class loading mode*/ Public classClassloadermain { Public Static voidMain (string[] args) {classloadermain CLM=NewClassloadermain (); //2. Using ClassLoaderclm.loadclassmethod2 (); }     Public voidloadClassMethod2 () {ClassLoader cl= This. GetClass (). getClassLoader (); Try{Class<?> clazz = Cl.loadclass ("Org.burning.sport.javase.classloader.ClassLoaderModel"); Classloadermodel CLM=(Classloadermodel) clazz.newinstance ();        System.out.println (CLM); } Catch(Exception e) {e.printstacktrace (); }    }}
3. Use classes. Class
 PackageOrg.burning.sport.javase.classloader;/*** Class loading mode*/ Public classClassloadermain { Public Static voidMain (string[] args) {classloadermain CLM=NewClassloadermain (); //3. Use classes. Class Modeclm.loadclassmethod3 (); }     Public voidloadClassMethod3 () {Class<?> clazz = Classloadermodel.class; Try{Classloadermodel str=(Classloadermodel) clazz.newinstance ();        System.out.println (str); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); }    }}
4. GetClass method of using object
 PackageOrg.burning.sport.javase.classloader;/*** Created by Lisen on 2017/7/27. * Class Loading mode*/ Public classClassloadermain { Public Static voidMain (string[] args) {classloadermain CLM=NewClassloadermain (); //4. GetClass method for using objectsclm.loadclassmethod4 (); }     Public voidloadClassMethod4 () {Classloadermodel CLM=NewClassloadermodel (); Class<?> Clazz =Clm.getclass (); Try{Classloadermodel ss=(Classloadermodel) clazz.newinstance ();        SYSTEM.OUT.PRINTLN (ss); } Catch(Exception e) {e.printstacktrace (); }    }}

Https://gitee.com/play-happy/base-project

javase-Reflection-Four ways to get classes or objects

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.