About Java reflect

Source: Internet
Author: User

The cornerstone of Reflection class class
    • Comparison question: The person class represents people, its instance object is Zhang San, John Doe such a specific person, Java program Java classes belong to the same class of things, the Java class name describing such things is class. Contrast question: What kind of a person does a lot of people say? What kind of class does a large number of Java classes represent?
      • Person
      • Java class
    • class represents the Java class, and what are the individual instance objects corresponding to each other?
      • A byte code corresponding to each class in memory, for example, the byte code of the Person class, the byte code of the ArrayList class, and so on.
      • A class is loaded into memory by the class loader, occupies a piece of storage space, the contents of this space is the class bytecode, different classes of bytecode is different, so they are in memory of the content is different, this space can be used by each object to represent, these objects are obviously the same type, What is this type?
    • How to get the instance object (class type) corresponding to each byte code
      • Class name. Class, for example, System.class
      • Object. GetClass (), for example, New Date (). GetClass ()
      • Static Class.forName ("class name"), for example, Class.forName ("java.util.Date");
        • Class.forName () Gets the byte code condition:
          1, bytecode has been loaded into the Java virtual machine, to get the byte code
          2, Java Virtual machine has not generated bytecode loaded with the ClassLoader, loaded bytecode buffer into the virtual machine
    • Nine pre-defined class instance objects: Eight basic data types and void types
      • See Help for the Class.isprimitive method
      • Int.class = = Integer.type]
      1. Byte.class
      2. Char.class
      3. Short.class
      4. Int.class
      5. Long.class
      6. Float.class
      7. Double.class
      8. Boolean.class and
      9. Void.class)
    • Class instance object of array type
      • Class.isarray ()
    • In summary, as long as the types that appear in the source program have their own class instance objects, for example, int[],void ...

1. TheJava class is used to describe the generality of a class of things, what the properties of such things are, and what the value of this property is, is determined by the instance object of the class, and different instance objects have different property values. Each Java class in a Java program, whether they belong to the same kind of thing, is it possible to use a class to describe such things?

The name of this class is class, pay attention to the difference between the lowercase class keyword OH. What kind of information does the class describe?

The name of the class, the Access property of the class, the package name to which the class belongs, a list of field names, a list of method names, and so on.

To learn reflection, you must first understand class. Write the following code to compare and understand:

        /* Person p1 = new Person ("Zhangsan");        person P2 = new Person ("Lisi");         */        /* Class x1 = Vector class byte code         in memory Class x2 = The byte code of the date class in memory *        /= Vector.  Class;         = Date. class;

Each Java class is an instance object of class, and their contents are different, but they have the same characteristics, for example, there are methods, fields, a parent class, and a package.

2. ****** must draw a picture when lecturing: the bytecode of multiple classes is loaded into memory, a block space is added in memory to represent the bytecode, and then an ellipse is used to represent the instance object created by the bytecode, and the code is monitored to show that the bytecode is only loaded once. The construction method of the instance object it constructs is called several times.

Use the following code to further illustrate what an instance of class is? is a byte code, a class usually has only one byte code in the virtual machine:

New== Date. class  null= Class.forName ("java.util.Date");    if (clazz1==clazz2)  {      System.out.println (Clazz1.getname ());  }   if (clazz1==clazz3)  {      System.out.println (Clazz1.getname ());  

3. A strange question: The bytecode is loaded and methods such as its getmethods are called, but the static code block of the class is not seen to be executed, and the static code is executed only when the first instance object is created.

To be exact, a static block of code is not invoked when the class is loaded, but only when the first instance object is created.

    • Reflection is the mapping of the various components in the Java class into the corresponding Java classes.
        • For example, a Java class is represented by an object in a class class: member variables, methods, construction methods, packages, and so on, are represented by a Java class, like a car is a class, the engine in a car, a gearbox, etc. are also classes. The class class that represents the Java class obviously has to provide a series of methods to obtain the variables, methods, constructs, modifiers, packages and other information, which are represented by the instance object of the corresponding class, which are field, method, Contructor, package, etc.
    • Each member of a class can be represented by an instance object of the corresponding reflection API class.
        • What is the use of these instance objects after they have been called by the method of class to get these instance objects? How to use it? This is the point of learning and applying reflection.

Constructor class

  • The constructor class represents a construction method in a class
  • Get all of the constructor methods for a class:
    • Example:
      Constructor [] constructors= class.forname ("java.lang.String"). GetConstructors ();

  • To get a construction method:
    • Example:
      Constructor Constructor = Class.forName ("java.lang.String"). GetConstructor (StringBuffer.  Class);

  • The type to use when obtaining the method
  • To create an instance object:
    • Usual way:
      New String (new StringBuffer ("abc"));
    • Reflection mode:
      String str = (string) constructor.newinstance (new StringBuffer ("abc"));
    • Use the same type of instance object as above when calling the obtained method
  • Class.newinstance () Method:
      • Example: String obj = (string) class.forname ("java.lang.String"). newinstance ();
      • The method internally obtains the default construction method, and then creates the instance object with the constructor method.
      • How is the specific code inside the method written? A caching mechanism is used to save the instance object of the default constructor method.

2. A class has multiple construction methods, and in what way can you tell which of these methods you want to know? Depending on the number and type of arguments, for example, the args parameter in Class.getmethod (Name,class ... args) represents a list of the types of each parameter of the method that you want to get. Emphasis: What is the method of parameter type representation? Use the class instance object. For example:

Int.class, (int []). Class

int [] ints = new Int[0];

Ints.getclass ();

The constructor object represents a construction method, what do you think the constructor object will have? Get the name, get the class that belongs to, produce the instance object.

3. When you create an instance object, you should first give an example of what is normally done, string str = new String (New StringBuffer ("abc")), and in the source program the traditional way of code is given in order to compare the reminder reader to the viewer.

And say how to do it with reflection.

String str = (string) constructor.newinstance (/* "abc" */new StringBuffer ("abc"));

System.out.println (str);

When you create an instance object by means of a reflection, you first deliberately use string as a parameter to pass in, according to the error to let everyone feel that it is actually the construction method, and then to pass a stringbuffer type parameter in, String str = (string) Constructor.newinstance (/* "abc" */new StringBuffer ("abc"));

Like, I call a person eats the grass the dinosaur, waits for it to eat the thing, I must give him the real life to go.

About Java reflect

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.