Introduction to the Java reflection mechanism

Source: Internet
Author: User

1. Byte code. The so-called bytecode is that when a Java virtual machine loads an object of a class, it first needs to compile the source code of that class in the hard disk into the binary code (bytecode) of class file, then load the bytecode of the class file into memory before creating the object of the class.

2.java reflection is based on class (note that it is not a lowercase class), and the Class class instance represents a byte code in memory. The common way to get class objects is as follows (the first method for the object, and the second method for the class):

[Java]View Plaincopyprint?
    1. Dog dog = new Dog ();
    2. Class Dogclass = Dog.getclass ();
    3. Class Dogclass = Class.forName ("Dog");
    4. Class dogclass = Dog.class;

3. Gets the reflection's constructor. With the basic class, to get the object of the class, we first get the constructor of the class, where the GetConstructor method of the class is used primarily, as follows:[Java]View Plaincopyprint?
  1. <span style= "WHITE-SPACE:PRE;" > </span>//using StringBuffer to construct string objects
  2. String str1 = new String (New StringBuffer ("Hello"));
  3. Gets the corresponding constructor object of the String class
  4. Constructor C1 = String.class.getConstructor (Stringbuffer.class);
  5. Error, the type of the argument here must be the same as the parameter type (and Stringbuffer.class) when the constructor is obtained
  6. String str2 = (string) c1.newinstance (new String ("World"));
  7. That's right
  8. String str3 = (string) c1.newinstance (New StringBuffer ("World"));
  9. System.out.println (STR3);


4. Gets the reflected field.

[Java]View Plaincopyprint?
  1. <span style= "FONT-SIZE:18PX;" >import Java.lang.reflect.Field;
  2. Class point{
  3. public int x;//here is public
  4. private int y;//Note this is private.
  5. Point (int x,int y) {
  6. this.x = x;
  7. This.y = y;
  8. }
  9. }
  10. public class Reflecttest {
  11. public static void Main (string[] args) throws Exception {
  12. Point P = new Point (2,3);
  13. GetField can only get fields of public
  14. Field fieldx = P.getclass (). GetField ("X");
  15. Get the value of X
  16. System.out.println (Fieldx.get (p));
  17. Getdeclaredfield can get all fields
  18. Field Fieldy = P.getclass (). Getdeclaredfield ("Y");
  19. Set properties for Y
  20. Fieldy.setaccessible (TRUE);
  21. System.out.println (Fieldy.get (p));
  22. }
  23. }</span>

5. Get the method of reflection.
[Java]View Plaincopyprint?
    1. public static void Main (string[] args) throws Exception {
    2. String S1 = "Hello";
    3. parameter is function name, function parameter (variable length)
    4. Method m = S1.getclass (). GetMethod ("CharAt", Int.class);
    5. Parameters are the objects to invoke, as well as the parameters of the function. Here, if the first argument is null, it means that the static method of the class is called
    6. System.out.println (M.invoke (S1, 1));
    7. }

Introduction to the Java reflection mechanism

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.