Java class objects use

Source: Internet
Author: User
Tags class definition

When learning the reflection mechanism, summarize how to get the class object:
The first way: to get objects by the class itself
class<?> classname = This.getclass (); or This.class.

The second way: Get the parent class object through an instance of the child class
ClassName cn = new ClassName ();
UserClass = Cn.getclass ();
class<?> Subuserclass = Userclass.getsuperclass ();

Third Way: Get objects by class name Plus. class
Class<?> Forclass = **.**. Classname.class; (class in Package path Plus. Class)

Fourth way: Get an object from a string of class name
class<?> forname = Class.forName ("**.**. ClassName ");
This approach is commonly used in JDBC.


Class<?> and Class differences:
In the class<?>? is a wildcard, in fact, is to represent any class that conforms to the conditions of the generic class definition, and the direct use of the class effect is basically consistent, but this is more canonical, in some types of conversion can avoid unnecessary unchecked errors.


The role of the class object: You can view the class
Class Loader Object
Reflection: Get object construction methods, properties, methods and annotations, note that the annotations here are the annotations of the class itself, rather than the annotations of the internal properties or methods, to get the corresponding method through the annotations, you can traverse the method and then judge the time to include the annotation, and the property and method object itself is the annotation property.
You can create an instance: Class.newinstance ()

Java class objects use

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.