The reflection of the Java Foundation

Source: Internet
Author: User

The Reflection library provides a very rich and well-designed toolset for writing programs that dynamically manipulate Java code. A program capable of analyzing classes becomes reflective (reflective). The reflection mechanism is extremely powerful and can be used to: the ability to analyze classes in operation, to view objects in run, to implement common array manipulation code, and to use the method object, which is much like a function pointer in C + +.

During a program run, the Java Runtime system always maintains a type identity known as the runtime for all objects. This information tracks the classes that each object belongs to. The virtual machine uses the run-time type information to select the appropriate method to execute. However, this information can be accessed through a specialized Java class. The classes that hold this information are called class. The GetClass () method in the object class returns an instance of class type.

Class is a java.lang in a package that can help a program create an instance of another class or obtain internal information about an object of another class.

Instantiating an object using class

The most common way to create objects is to use the new operator and the constructor of the class, and you can actually get an instance of a class using the class object. The steps are as follows:

    1. Class methods are used to obtain a class object that is related to certain classes (the class specified by the parameter className), public static class forname (String className) throws ClassNotFoundException. The above method returns a class object that is related to the classname specified by the parameter. If the class is in a package, className must have a package name, for example, ClassName = "Java.util.Data".
    2. The class object obtained in step 1 calls public object Newinstance () throws Instantiationexception, The Illegalaccessexception method allows you to get an object of the ClassName class.

It is important to note that when you use a class object to invoke Newinstance () to instantiate an object of a classname class, the ClassName class must have a parameterless construction method.

Get information about a class

When a class is loaded and an object is created, an object of type class related to that class is automatically created, and any object call GetClass () method can get a class object associated with the object, and the class object calls the following method to obtain information about the object, such as , the name of the class that created the object, the name of the method in the class, the name of the member variable, and so on.

1 Importjava.lang.reflect.*;2 Importjava.util.Date;3 4  Public classExample {5      Public Static voidMain (string[] args) {6Date Date =NewDate ();7Class cs =Date.getclass ();8String ClassName =cs.getname ();9Constructor[] con =cs.getdeclaredconstructors ();Tenfield[] field =cs.getdeclaredfields (); OneMethod[] Method =cs.getdeclaredmethods (); A  -System.out.println ("ClassName:" +className); -SYSTEM.OUT.PRINTLN ("Class constructors:"); the          for(Constructor c:con) { - System.out.println (c.tostring ()); -         } -System.out.println ("Class fields:"); +          for(Field F:field) { - System.out.println (f.tostring ()); +         } ASystem.out.println ("Class methods:"); at          for(Method m:method) { - System.out.println (m.tostring ()); -         } -     } -}

Reflection of the implementation of the common array operation code and use method is not used temporarily, for the time being do not write ...

The reflection of the Java Foundation

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.