Java reflection mechanism, java reflection

Source: Internet
Author: User

Java reflection mechanism, java reflection
JAVA reflection mechanism: "When a program is running, the program structure or variable type can be changed. This language is called a dynamic language ". Overview:

The JAVA reflection mechanism is in the running state. For any class, all attributes and methods of this class can be known. For any object, can call any of its methods and attributes. This kind of information obtained dynamically and the function of dynamically calling methods of objects is called the reflection mechanism of java language.

Function:

The Java reflection mechanism mainly provides the following functions: to judge the class to which any object belongs at runtime; to construct the object of any class at runtime; judge the member variables and methods of any class at runtime; call the methods of any object at runtime; generate a dynamic proxy.


Therefore, the JAVA reflection mechanism reflects the dynamic nature of Java.


What is the reflection mechanism of JAVA? Reflection Java programming language features: Allows Java programs to run to check itself or perform self-audit and can directly operate on internal property examples of the program to obtain and display the names of members of the Java class.
The actual application of Java capabilities may be much more fundamental in the programming language. For example, Pascal, C, or C ++, there is no way for the program to obtain Function Definition information.
The actual application of JavaBean reflection allows some tools to operate software components. Some tools dynamically load and obtain Java component (class) attributes through reflection.
JAVA reflection mechanism import java. lang. reflect .*;

/* The so-called reflection mechanism class calls the methods and attributes in the alternative class have not been defined.
* As long as you know that there is a method call in it, the call class needs to obtain the complete class name to create class objects.
* The object without parameter instantiation is called.
* The getDeclaredConstructor () method is required for parameter construction. The corresponding Constructor is used in the Constructor variable.
* Creating Object Instantiation involves the output of the constructor just now.
Reflection mechanism:
Call the China House object of the no-argument constructor instance;
A parameter is called to construct the Hua House object of the instance;
Call the toString Method
*/
Public class ReflectTestPro {
Public static void main (String [] args) throws Exception {
Class c = Class. forName ("chap06.House"); // path

// New House ();
// Call the China House object of the no-argument constructor instance;
Object h1 = c. newInstance (); // polymorphism represents the parent class and subclass.
System. out. println (h1 );

// Call the amarraylist object of the constructor instance;
Class c1 = Class. forName ("java. util. ArrayList"); // The Class name must be complete.
Object h2 = c1.newInstance (); // It is equivalent to new ArrayList ();
System. out. println (h2 );

// Call a parameter to construct the Hua House object of the instance;
Constructor cons = c. getDeclaredConstructor (new Class [] {String. class,
String. class, double. class}); // The parameter type in the constructor must be
Object h3 = cons. newInstance (new Object [] {"shanghai", "3-2-1 ",
New Double (100.0)}); // instantiate a class instance
// Equivalent to the conventional new House ("shanghai //," 3-2-1 ", 100)
System. out. println (h3 );

// Call the China Date object of the constructor instance with Parameters
Class c4 = Class. forName ("java. util. Date ");
Constructor cons4 = c4.getDeclaredConstructor (new Class [] {long. class });
Object h4 = cons4.newInstance (new Object [] {new Long (999999l )});
System. out. println (h4 );

// Call toString
Method m = c. getDeclaredMethod ("toString", new Class [] {});
String s = (String) m. invoke (h3, new Object [] {}); // Stirng
// S = h3.toString ();
System. out. println (s );

}
}

Related Article

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.