Get the App
SLTechnology News&Howtos  ›  Development  › 

What is JAVA reflection

Shulou Source: shulou.com Published: 2022-06-03 06:44:01 11月15日 Update

This article mainly explains "what is JAVA reflection". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is JAVA reflection".

What is reflex?

Reflection is to map the components of the Java class into Java objects one by one. That is, in the running state, for any class, you can know all the properties and methods of that class; for any object, you can call any of its methods and properties. This function of dynamically obtaining information and dynamically calling object methods is called Java reflection mechanism.

How to get the bytecode file of a class using the full path of the class Class clazz = Class.forName ("com.xuzhu.reflect.Person"); type .classClass clazz = Person.class; object .getClass () Person person = new Person (); Class clazz = person.getClass (); get all attributes of the class Field [] declaredFields = clazz.getDeclaredFields (); for (Field field: declaredFields) {String fieldName = field.getName (); System.out.println ("fieldName:" + fieldName) } get all the methods of the class Method [] declaredMethods = clazz.getDeclaredMethods (); for (Method method: declaredMethods) {String methodName = method.getName (); System.out.println ("methodName:" + methodName);} call method / / call setAge ()\ getAge () method Method setAge = clazz.getDeclaredMethod ("setAge", Integer.class); Object instance = clazz.newInstance (); setAge.invoke (instance, new Integer (1)); Method getAge = clazz.getDeclaredMethod ("getAge") System.out.println (getAge.invoke (instance)); thank you for reading, the above is the content of "what is JAVA reflection". After the study of this article, I believe you have a deeper understanding of what is JAVA reflection, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Reflection methods objects properties learning content dynamics that is information functions bytes ideas situations components files articles more mechanisms states knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS NVidia Microsoft Huawei