[Java reflection mechanism] _ notes for using the class

Source: Internet
Author: User

[Java reflection mechanism] _ notes for using the class

Objectives of this chapter:
You can use the class to instantiate an object.
You can use the constructor class to call a constructor with parameters to instantiate an object.

Details:
Class is the source of reflection. It can not only obtain information about the class where the object is located, but also directly instantiate the object using the class method. Normally, the new keyword is used for Object Instantiation, if the class object has been instantiated, you can use

Public t new instance () throws instantiationexception, illegalaccessexception
Instantiate object

Package Org. lxh. demo15.instancedemo; class person {private string name; private int age; Public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = age;} Public String getname () {return this. name;} public int getage () {return this. age;} Public String tostring () {return "name:" + this. name + ", age:" + this. age ;}} public class instancedemo01 {public static void main (string [] ARGs) {class <?> C = NULL; // declare the class object try {c = Class. forname ("org. lxh. demo15.instancedemo. person ");} catch (classnotfoundexception e) {e. printstacktrace ();} person per = NULL; // declare the person object try {per = (person) C. newinstance ();} catch (exception e) {e. printstacktrace ();} per. setname ("Li Xinghua"); // set the name per. setage (30); // set the age system. out. println (PER); // content output, call tostring }}

Through the above code, we can find that even if the keyword new object is not used, it can be instantiated and reflected.
However, when using the preceding operations, you must note that the class in the operation must have a non-argument constructor. Otherwise, it cannot be instantiated.

Package Org. lxh. demo15.instancedemo; class person {private string name; private int age; Public Person (string name, int age) {This. setname (name); this. setage (AGE);} public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = age;} Public String getname () {return this. name;} public int getage () {return this. age;} Public String tostring () {return "name:" + this. name + ", Age: "+ this. Age ;}}; public class instancedemo02 {public static void main (string ARGs []) {class <?> C = NULL; try {c = Class. forname ("org. lxh. demo15.instancedemo. person ");} catch (classnotfoundexception e) {e. printstacktrace ();} person per = NULL; // declare the person object try {per = (person) C. newinstance (); // instantiated object} catch (exception e) {e. printstacktrace ();} per. setname ("Niuer graze"); // set the name per. setage (30); // set the age system. out. println (PER); // content output, call tostring }}

The following error occurs:
Java. Lang. instantiationexception: org. lxh. demo15.instancedemo. Person

Therefore, it is found that the above method still requires the support of constructor methods in the class, in line with the requirements of Object Instantiation.
If you want to solve this problem, you must specify the constructor to be called and the semi-pass parameter. However, from the actual development perspective, when using reflection to instantiate an object, it is better to have a non-Argument Structure in the class. This operation is reasonable.
To call an API with parameters, follow these steps:

Note: The class of the instantiated object must have a construction method without parameters. If it does not exist, it cannot be instantiated.
The above programs are not unsolved, but can also be instantiated in other ways. They only need to explicitly call the constructor in the class during the operation, the parameter can be instantiated only after it is passed in. The procedure is as follows:

1. Use getconstructors () in the class to obtain all the constructor methods of this class.
2. Pass an array of objects to the constructor, which contains the parameters required by the constructor.
3. The object is then instantiated through the constructor.

There is a method in the constructor class:

Public t newinstance (object... initargs)
Throws instantiationexception,
Illegalaccessexception,
Illegalargumentexception,
Invocationtargetexception
The initialization parameters are passed to instantiate the object.

Explicitly call construction with parameters and instantiate the object:

Package Org. lxh. demo15.instancedemo; import Java. lang. reflect. constructor; class person {private string name; private int age; Public Person (string name, int age) {This. setname (name); this. setage (AGE);} public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = age;} Public String getname () {return this. name;} public int getage () {return this. age;} Public String Tostring () {return "name:" + this. name + ", age:" + this. age ;}} public class instancedemo03 {public static void main (string ARGs []) {class <?> C = NULL; try {c = Class. forname ("org. lxh. demo15.instancedemo. person ");} catch (classnotfoundexception e) {e. printstacktrace ();} person per = NULL; // declare the person object constructor <?> Cons [] = NULL; cons = C. getconstructors (); try {per = (person) Cons [0]. newinstance ("Li Xinghua", 30); // instantiate object} catch (exception e) {e. printstacktrace ();} system. out. println (PER); // content output, call tostring ()}}

However, from the actual point of view, if you want to use reflection to instantiate an object, it is best to have a no-Argument Structure in the class.

Summary:
1. The functions in this chapter are the most used functions of the class, and the Development Mode that users often use during development
2. When using class to instantiate an object, you must ensure that there is a no-Argument Structure in the class; otherwise, the object cannot be used.
3. If you want to call a constructor class to instantiate an object, you must use the constructor class. This class indicates the constructor and pass the required content through variable parameters.

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.