[Java reflection mechanism] _ understanding class notes

Source: Internet
Author: User

[Java reflection mechanism] _ understanding class notes

Objectives of this chapter:
Measure the test taker's knowledge about the functions of the getclass () method in the object class.
Java reflection mechanism

3. Details

Under normal circumstances, an object can be instantiated only after the complete path of a class is known. However, in Java, an object can be used to locate the class information, this is actually the function of the class.

Package Org. lxh. demo15.getclassdemo; Class X {}; public class getclassdemo01 {public static void main (string ARGs []) {x = new x (); // instantiate the object of the X class. System. Out. println (X. getclass (). getname (); // obtain the class of the object }}

At this time, all operations are reversed.

Support for object classes

The following methods are defined in the object class. This method will be inherited by all subclasses.

Public final class getclass ()

The type of the return value of the above method is a "class" class. In fact, this class is the source of Java reflection. In fact, the so-called reflection is well understood from the running results of the program, that is: the class name can be obtained through object reflection.

Class

The class itself represents the class itself. Through the class, you can obtain the complete structure of a class, including the method definition and attribute definition in this class.

Common Methods of class classes

This class does not find any constructor In the JDK document, so this kind of constructor is privatized.

There are three methods to instantiate a class object:
First: Use the forname () method
Type 2: class. Class
Third: object. getclass ()

Package org. lxh. demo15.getclassdemo; Class X {} public class getclassdemo02 {public static void main (string ARGs []) {class <?> C1 = NULL; Class <?> C2 = NULL; Class <?> C3 = NULL; try {// The following operation form is the most commonly used form in development C1 = Class. forname ("org. lxh. demo15.getclassdemo. X ");} catch (classnotfoundexception e) {e. printstacktrace ();} C2 = new x (). getclass (); // instantiate C3 = x through the method in the object class. class; // pass the class. class instantiation system. out. println ("Class Name:" + c1.getname (); system. out. println ("Class Name:" + c2.getname (); system. out. println ("Class Name:" + c3.getname ());}}

After the class can be instantiated, further reflection operations can be performed.

Summary:
1. Understand the role of the class: the source of reflection
2. Three Class class instantiation methods, which should be focused on "forname ()", class. Class

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.