Reflection reflects the dynamic mechanism in Java

Source: Internet
Author: User

In Java, no matter how many objects of a class are generated, these objects correspond to the same class object.

The Dynamic Language is:ProgramLanguage that allows you to change the program structure or variable type during runtime
Ruby is a dynamic Language
Java is a static Language

Reflection reflection is a dynamic mechanism in Java
Java. Lang. Reflect
The class is located in Java. Lang.
Field
Method
Constructor
Array

Simple Example:
Obtain the class object corresponding to the class
Class classtype = test. Class;
Create a class instance
Object o = test. newinstance ();

Obtains a specified method object.
  Method addmethod = classtype. getmethod ("add", new class [] {Int. Class, Int. Class });
The first parameter is the method name, and the second parameter is the class object for passing the Parameter

Invoke calls the target method
Object result = addmethod. Invoke (O, new object [] {1, 2 });

1. To use reflection, first obtain the class object corresponding to the class to be processed.

2. Three common methods for obtaining class objects corresponding to a class or an object
A) use the static class method forname, class. forname ("Java. Lang. String ");
B) Use the. Class syntax of the class: String. Class;
C) use the getclass () method of the object: String S = "AA"; Class <?> Clazz = S. getclass ();

3. If you want to generate an object using the class constructor without parameters, we have two methods:
A) first obtain the class object, and then generate it directly using the newinstance () method of the Class Object:
Class <?> Classtype = string. Class; object OBJ = classtype. newinstance ();

B) first obtain the class object, then obtain the constructor object used by the object, and then generate it using the newinstance () method of the constructor object;
Class <?> Classtype = Customer. Class;
Constructor cons = classtype. getconstructor (new class [] {});
Object OBJ = cons. newinstance (new object [] {});

4. If you want to generate an object using the constructor with parameters of the class, you can only use the second method.
Class <?> Classtype = Customer. Class;
Constructor cons = classtype. getconstructor (new class [] {sring. Class, Int. Class });
Object OBJ = cons. newinstance (new object [] {"hello", 3 });

Related Article

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.