Java reflection defines, obtains Class three methods, class Three Methods

Source: Internet
Author: User

Java reflection defines, obtains Class three methods, class Three Methods

Reflection mechanism definition:

When running (dynamic), all attributes and methods of this class can be obtained for any class.

Any attributes and methods of an object can be called.

 

The Class is the origin of the reflection mechanism. We can obtain three methods for Class objects:

First: Obtained by Class Name

Class <?> Class = ClassName. class;

Method 2: Get through the full path of the Class Name:

Class <?> Class = Class. forName ("class Name full path ");

Third: Get Through instance object:

Class <?> Class = object. getClass ();

Sum up the differences between the three methods through code: (add tomorrow)

1 class Test {2 static {3 System. out. println ("static code block"); 4} 5 {6 System. out. println ("dynamic code block"); 7} 8}
1 public class ClassCreate {2 public static void main (String [] args) {3 Class <?> Test2 = Test. class; 4 System. out. println ("test"); 5} 6}

 

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.