Java reflection mechanism (ii)

Source: Internet
Author: User

  The Core Java Volume I---Fundamentals describes three ways to create class objects. This article will illustrate the three ways to create class objects---create classes.

First, public final native class<?> GetClass ()

The GetClass () method in the object class returns an instance of the class type, which is a specific class (an object of Class).

1) class is not parameterized, i.e. API is Class GetClass ()



output result :class reflection. Employee

Return value Analysis: Class+ classes fully qualified name

2) class parameterization, constraining incoming parameter inheritance types

3) class parameterization, specifying specific constraint parameters

Code Analysis : getclass () The default return value is Class<?>, that is, the parameter type is undefined. You must therefore force the type to convert to Class<employee>.

If you need to remove the keyword class from the returned results, use the class API---String getName ()

1  PublicString GetName () {2     if(Name = =NULL)3Name =GETNAME0 ();4     returnname;5 }6 7 //cache the name to reduce the number of calls into the VM8 Private transientString name;9 Private nativeString GETNAME0 ();

output result :Reflection. Employee

Second, public static class<?> forname (String className)

The class object that corresponds to the name of the Java.lang.Class is obtained by calling the static method of the forname.

 1  public  static  Class<?> forname (String className)  2  throws     ClassNotFoundException    { 3  return  FORNAME0 (  ClassName  , true  ,classloader.getcallerclassloader ());  4 } 

The parameter passed in string classname must be a fully qualified name , which is the package name plus the class name.

Description The Forname method can execute successfully only if ClassName is the same class name or interface name as the GetName return format , otherwise the Forname method throws a ClassNotFoundException exception. Another point is that the Forname method is used to load classes manually.

Output Result:class reflection. Employee

    

  Summary : The string format returned by GetName is the package name + class name, excluding the project name. Therefore, the Forname string format is also the package name + class name. This is slightly different from using the Forname static method to load the driver class in JDBC .

Third, class<t> T.class, where T represents the data type

Output Result :

Span style= "color: #ff0000;" >class reflection. Employee
int
class java.lang.Integer
interface java.lang.Runnable

Analysis code: I used to think that the class here is a property, but when I wrote this blog, I superficial understanding that the previous view was wrong. First, from the point of view of the eclipse code,class is the keyword , and the second int is the basic data type and cannot have attributes. Several reasons can be summed up as follows: if T is an arbitrary java type, T.class will represent the type to which it matches .

Iv. Summary and Reflection

  A class object actually represents a data type (Basic or reference data type), and this type is not necessarily a class .

Of course, it can be seen from here that class<int> allows parameters to be basic data types, unlike generic requirements that must be reference data types. Can be remembered as a special case.

Reference Documents :

Core Java Volume I---Fundamentals cay S. Horstmann Gary Cornell    

  Statement: The article is the author of the original finishing, if you need to reprint please contact me.

Java reflection mechanism (ii)

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.