Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The function and example of Java reflection mechanism

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "Java reflection mechanism function and example", in daily operation, I believe many people in Java reflection mechanism function and example problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Java reflection mechanism function and example" doubts helpful! Next, please follow the small series to learn together!

1)obtain a class

A. Use getClass() (every class has this function)

i. String str = "abc";

ii. Class c1 = str.getClass();

B. Use Class.forName() (static method most often used)

i. Class c1 = Class.forName ("java.lang.String");

ii. Class c2 = Class.forName ("java.awt.Button");

C. Use.class syntax

i. Class c1 = String.class;

ii. Class c2 = java.awt.Button.class;

iii. Class c4 = int.class;

iv. Class c5 = int[].class;

D. Use TYPE syntax ( primitive wrapper classes TYPE syntax for wrapper classes)

i. Class c1 = Boolean.TYPE;

2)obtain attribute

There are four ways to get properties, mainly Java.lang.class

Public Field getField

(String name)

Returns a Field object that reflects the specified public member fields of the class or interface represented by this Class object

public Field[] getFields()

Returns an array of Field objects reflecting all accessible common fields of the class or interface represented by this Class object

Public Field

getDeclaredField(String name)

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object

public Field[]

getDeclaredFields()

Returns an array of Field objects reflecting all fields declared by the class or interface represented by this Class object

3)Method of acquisition ()

There are four ways to get a method, see Java.lang.class

public Method

getMethod(String name,

... )

Returns a Method object that reflects the specified public member methods of the class or interface represented by this Class object

public Method[] getMethods()

Returns an array of Method objects reflecting the common member methods of the class or interface represented by this Class object, including those declared by and inherited from superclasses and superinterfaces

public Method

getDeclaredMethod(String name,…)

Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object

public Method[]

getDeclaredMethods()

Returns an array of Method objects reflecting all methods declared by the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods

4)Gets the constructor of a class

There are four ways to get a method, see Java.lang.class

public Constructor

getConstructor(Class... )

Returns a Constructor object that reflects the specified public constructor method of the class represented by this Class object

public Constructor[]

getConstructors()

Returns an array of Constructor objects that reflect all the common constructor methods of the class represented by this Class object

Public Constructor

getDeclaredConstructor(Class...)

Returns a Constructor object that reflects the specified constructor method for the class or interface represented by this Class object

public Constructor[]

getDeclaredConstructors()

Returns an array of Constructor objects that reflect all constructor methods declared for the class represented by this Class object. They are public, protected, default (package) access, and private construction methods

Example code:

import java.lang.reflect.Constructor; public class TestConstructor { /** * @param args * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Class classType=Class.forName(args[0]); Constructor Constructor= classType.getConstructor(); System.out.println(Constructor.toString()); } }

5)Create an instance of a new class

Call the newInstance method of the Class object of the class

import java.lang.reflect.Constructor; public class TestConstructor { /** * @param args * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Class classType=Class.forName(args[0]); //Constructor Constructor= classType.getConstructor(); Object inst=classType.newInstance(); System.out.println(inst); } }

Call newInstance method of default Constructor object

import java.lang.reflect.Constructor; public class TestConstructor { /** * @param args * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Class classType=Class.forName(args[0]); Constructor Constructor= classType.getConstructor(); Object inst=Constructor.newInstance(); System.out.println(inst); } }

Call newInstance method of Constructor object with parameter

Class classType=User.class Constructor constructor2 = classType.getDeclaredConstructor(int.class, String.class); Object inst = constructor2.newInstance(1, "123"); System.out.println(inst); At this point, the study of "Java reflection mechanism functions and examples" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report