Java Black Magic-reflection mechanism-01

Source: Internet
Author: User

In the Java Help document, there is a class class in the Java.lang package, note that "C" here is capitalized, so this is not a declaration of the class, but a real class. In the Java Help documentation, the class class is defined like this:

Public final class Class<t>extends objectimplements Serializable, An instance of the class Genericdeclaration, type, Annotatedelement

represents classes and interfaces in a running Java application. An enumeration is a class, and a comment is an interface. Each array belongs to a class that is mapped to a class object, and all arrays that have the same element type and number of dimensions share the class object. The basic Java types (Boolean, Byte, char, short, int, long, float, and double) and the keyword Void are also represented as Class objects. Class has no public constructor method. Class objects are constructed automatically by the Java virtual machine and by invoking the DefineClass method in the class loader when the class is loaded.

  in Java, each class has a corresponding class object. That is, when we write a class, after the compilation is complete, in the generated. class file, a class object is generated that represents the type information for this class. The class class is not created by us, it is created by a Java virtual machine when we generate a. class file, and we can obtain this class instance in several ways. Here are some ways to do this:

(1) Use the object invocation GetClass () method to get the class instance of the object,   
(2) using the static method of class Forname (), use the name of the class to get a class instance,
(3) Use the. class method to get the class instance, and for the wrapper class of the base data type, you can also use the. Type to get a class instance of the corresponding base data type. The following is an instance of

 PackageCom.aaron.reflect;classPoint {intAge ;  Public voidsay () {System.out.println (123); }} Public classTest { Public Static voidMain (String args[]) {System.out.println (NewPoint (). GetClass ());//class Com.aaron.reflect.Point and there are methods say//class<?> C11 = (class<?>) new Point (); you can't convert that.Point pt =NewPoint (); Class<?> C1 = Pt.getclass ();//This method is often common, c1, and does not have this methodSystem.out.println ("+c1.getname" ());//Com.aaron.reflect.Point        Try{//Forname throws an exception so it needs to be captured and the path of the package must be addedclass<?> C2 = class.forname ("Com.aaron.reflect.Point"); System.out.println ("101" +c2.getname ());//Com.aaron.reflect.Point//System.out.println (C2.say ()); There is no such method.}Catch(Exception e) {e.printstacktrace (); } Class<?> C3 = Point.class; System.out.println ("+c3.getname" ());//Com.aaron.reflect.Pointclass<?> C4 =int.class; System.out.println ("001" +c4.getname ());//intclass<?> c5 =Integer.type; System.out.println ("002" +c5.getname ());//intclass<?> C6 = Integer.class;        //Each class can be viewed as an object of class, using the. class method to get the class instance System.out.println ("003" +c6.getname ());//003java.lang.integer    }}


The Java Virtual machine (JVM) checks whether a class object of that type has been loaded during run time if we are going to produce an object of that kind. If it is not loaded, the JVM will find the. class file and load it based on the name of the class. Once a class object of a type has been loaded into memory, it can be used to produce all objects of that type. In other words, not all of the. class files in Java will be loaded into memory and will be loaded only when needed.

The class class has a method Newinstance (), which creates an object without knowing the name of the class. When using the Newinstance () method, it should be noted that this method is called by default to construct without parameters. If we just go to create a constructor without parameters, then this method is not very useful, if we want to invoke the constructor with parameters, here we need to use the inverse color API. For an introduction to the anti-color API, check out one of my previously reproduced articles (http://blog.csdn.net/mengxiangyue/article/details/6825923). Sometimes we get a class that is sent over the network, we need to create an object, but we don't know how this class is implemented. Here we can use the inverse color API plus the class newinstance () to create an object without knowing the class. For the purpose of this class now know so much, if there is anything, I hope there is a hero guidance.

Java Black Magic-reflection mechanism-01

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.