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

What is the meaning of reflection in java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "what is the meaning of reflection in java", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the meaning of reflection in java" can help you solve your doubts.

I. Overview of reflection

Reflection (reflection) is regarded as the key of dynamic language. The reflection mechanism allows the program to obtain the internal information of any class with the help of Reflection API during execution, and can directly manipulate the internal properties and methods of any object. After the class is loaded, an object of type Class (there is only one Class object for a class) is generated in the method area of heap memory, which contains the structural message of the complete class. We can see the structure of the class through this object, the object is like a mirror, through this mirror to see the structure of the class, so we visually call it reflection.

Second, commonly used api

Java.lang.Class: represents a class

Java.lang.reflect.Method: a method that represents a class

Java.lang.reflect.Field: a member variable that represents a class

Java.lang.reflect.Constructor: a constructor that represents a class

Static Class forName (String name) returns the Class object of the specified class name name

Object newInstance () calls the no-parameter constructor and returns an instance of the Class object.

GetName () returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object

Class getSuperClass () returns the Class object of the parent class of the current Class object

Class [] getInterfaces () gets the interface of the current Class object

ClassLoader getClassLoader () returns the class loader of this class

Class getSuperclass () returns the Class representing the superclass of the entity represented by this Class

Constructor [] getConstructors () returns an array containing some Constructor objects

Field [] getDeclaredFields () returns an array Method of the Field object

GetMethod (String name,Class... ParamTypes) returns a Method object whose formal parameter type is paramType

Example:

Public class Person {private String name; private int age; public Person () {System.out.println ("Person class is initialized. ^ _ ");} public Person (String name, int age) {this.name = name; this.age = age;} public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age } @ Override public String toString () {return "Person {" + "name='" + name +'\'+ ", age=" + age +'}';}}

Reflection

Public class ClassDemo {public static void main (String [] args) throws Exception {/ / 1. Create Class object / / Class clazz = (Class) Class.forName ("Person"); Class clazz = Class.forName ("Person"); / / 2. Call its method / / 2. 1 through the Class object. Instantiate the object by reflection / / Person person = clazz.newInstance (); Object o = clazz.newInstance (); / / this name is equivalent to Person p = new Person () / / 2.2. Get the full class name of the class (package name + class name) String name = clazz.getName (); System.out.println (name); / / 2.3. Get the simple name of the class, which is the name of the class, without the package name System.out.println (clazz.getSimpleName ()); / / 2.4. Get the constructor Constructor [] constructors = clazz.getConstructors (); for (Constructor constructor: constructors) {System.out.println (constructor.getName ());} / / 2.5. Get the field Field [] fields = clazz.getFields (); for (Field field: fields) {System.out.println (field.getName ());} / / 2.6. Get method Method [] methods = clazz.getMethods (); for (Method method: methods) {System.out.println (method.getName ()) Three ways to create Class objects public class Demo {public static void main (String [] args) throws Exception {/ / first way: call Class.forName () Class clazz = Class.forName ("java.lang.String"); / / second way: create Class clazz1 = String.class through the bytecode file of the class / / third way: create String str = new String () by calling the object's getClass () method; Class clazz2 = str.getClass (); / / fourth: create ClassLoader loader = String.class.getClassLoader () through the class loader; Class clazz3 = loader.loadClass ("java.lang.String") }} after reading this, the article "what is the meaning of reflection in java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report