Methods for loading classes in reflection in Java

Source: Internet
Author: User

Reflection: There are three ways to load a class,

1. Use Class.forName ("Class name") method to invoke;
2. Class name. Classes Get
3. Using the object. GetClass () to get
Package Com.ma.reflection;import Org.junit.test;import Com.ma.bean.userbean;public class Demo1 {/** * Reflection: Methods for loading Classes */@ testpublic void Test1 () {//1. Called with the Class.forName ("Class name") method; try {class<?> c = class.forname ("Com.ma.bean.UserBean"); System.out.println (c);} catch (ClassNotFoundException e) {e.printstacktrace ();} 2. Class name. Classes Get class<?> cla1 =  userbean.class; System.out.println (CLA1);//3. With object. GetClass () get class<?> CLA2 =  new UserBean (). GetClass (); System.out.println (CLA2);}}

Userbean entity class

Package com.ma.bean;/** * UserBean entity class * @author MA * */public class UserBean {private string Id;private string name = "Zhang San" ;p rivate String password;public UserBean () {super (); System.out.println ("I am a non-parametric construction method");} Public UserBean (string ID, string name, string password) {super (); this.id = Id;this.name = Name;this.password = Password;s Ystem.out.println ("I am a constructive method");} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} @Overridepublic String toString () {return "UserBean [id=" + ID + ", name=" + name + ", password=" + password + "]";} Private UserBean (String name) {this.name = name; SYSTEM.OUT.PRINTLN (name);} }

  

Methods for loading classes in reflection 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.