A summary of Java Reflection Learning (Basic article)

Source: Internet
Author: User
Tags object object

Class is the core class in the reflection API, and he is located in Java.lang.Class

Lists some of the commonly used methods.

-GetName (): Get the full name of the class

-GetFields (): Gets the properties of the public type of the class

-Getdeclaredfields (): Get all the properties of the class

-GetMethods (): Method for obtaining the public type of a class

-Getdeclaredmethods (): Get all methods of class

-GetMethod (String name, class[] parametertypes): Gets the specific method of the class (the name parameter specifies the method name, and the Parametertypes parameter specifies the method parameter type)

-GetConstructors (): How to get the public type of a class

-GetConstructor (class[] parametertypes): Gets the specific construction method of the class (the Parametertypes parameter specifies the parameter type of the constructor method)

-Newinstance (): Creates an object of this class from the constructor of the class without parameters


If you want to use reflection, there are 2 key parts

1. Get the Class object

2. Obtaining an Object instance

These two sections are described below:


1. How to obtain a class object

3 common ways to get a class object that corresponds to one of the classes or objects

A) Use the static method of class Forname:

class.forname ("java.lang.String");

b) Use the class's. class syntax :

String.class;

c) Use the GetClass () method of the object (the method in the Java.lang.Object Class):

String s = "AA";

class<?> clazz = S.getclass ();


2. How to obtain an object instance

We all know that getting an object instance is going to be a new one, which is actually the construction method of the calling object

There are two different types of calling constructor method parameters:

A) Call the constructor without parameters:

1. Call the Newinstance () method of the class object:

class<?> ClassType = Classclass.forname ("java.lang.String");

Object object = Classtpye. newinstance ();

2. Call the Newinstance () method of the constructor object of the class object and pass an empty class object array as the parameter:

class<?> ClassType = Classclass.forname ("java.lang.String");

Constructor cons = Classtype.getconstructor (new class[]{});

Object object =cons.newinstance (new object[]{});

b) Call the constructor with parameters:

1. Call the Newinstance () method of the Class object's constructor object, passing a variable-length class object array as a parameter, this example passes string,int two parameters:

class<?> ClassType = Classclass.forname ("java.lang.String");

Constructor cons = Classtype.getconstructor (new Class[]{string.class, int.class});

Object Object =cons.newinstance (new object[]{"Hello", 3});


Here is a small demo, because it is too simple to paste the code here

This demo simple implementation of the above described some of the use of reflection, as a review to consolidate:

[Java]View Plaincopy print?
  1. Class Person {
  2. private Long ID;
  3. private int age;
  4. private String name;
  5. Public Person () {
  6. }
  7. Public person (String name, int age) {
  8. this.name = name;
  9. this.age = age;
  10. }
  11. @Override
  12. Public String toString () {
  13. return "Name=" +getname () +"age=" +getage () +"id=" +getid ();
  14. }
  15. public Long GetId () {
  16. return ID;
  17. }
  18. public void SetId (long id) {
  19. this.id = ID;
  20. }
  21. public int getage () {
  22. return age;
  23. }
  24. public void Setage (int.) {
  25. this.age = age;
  26. }
  27. Public String GetName () {
  28. return name;
  29. }
  30. public void SetName (String name) {
  31. this.name = name;
  32. }
  33. }
A very simple custom class, two constructor methods, and some get,set methods, and a rewrite of the ToString


The following is the code in the main method of invoking reflection, mainly is to call the person's parameter construction method with the reflection method to assign a value to Age,name, call SetID to assign the ID, and finally call the ToString method to print the result

[Java]View Plaincopy print?
  1. Public static void Main (string[] args) throws Exception {
  2. //Get the class object for person classes
  3. class<?> ClassType = class.forname ("person");
  4. //Call the two parameter construction method of the person class to build the object
  5. Constructor Constructor = classtype.getconstructor (new class[]{string.   class, int.class});
  6. Object object = Constructor.newinstance (new object[]{"Dean",25});
  7. //Get SetID method
  8. Method setId = Classtype.getmethod ("SetId", new class[]{long.   class});
  9. //Call the SetID method to set the ID
  10. Setid.invoke (object, new object[]{10});
  11. //Call ToString Output result
  12. Method toString = Classtype.getmethod ("toString", new class[]{});
  13. String result = (string) Tostring.invoke (object, new object[]{});
  14. SYSTEM.OUT.PRINTLN (result);
  15. }

The output is: Name=dean age=25 id=10


Recently watching the reflection of the video and materials, the purpose of writing this blog is to learn a summary of it.

In succession, I would like to update some of the deeper reflective knowledge I have summed up, I hope you can support

A summary of Java Reflection Learning (Basic article)

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.