Reflection is considered the key to a dynamic language. Reflection allows programs to use the Reflection API to retrieve the internal information of any class during execution and to directly manipulate the internal properties and methods of any object.

After the Class is loaded, an object of type Class (one Class object per Class) is generated in the method area of heap memory, which contains the complete structure information of the Class. We can see the structure of the class through this object. This object is like a mirror through which you can see the structure of the class, so we call it a reflection.

Normal mode: introduce the required package name → instantiate the object through new → get the instantiated object reflection mode: Instantiate the object → getClass() method → get the complete package name

Functionality provided by the Java reflection mechanism

Determine the class to which any object belongs at run time. Constructs an object of any class at run time. Determine which member variables and methods any class has at run time. Get generic information at run time. Call the member variables and methods of any object at run time. Annotations are processed at run time. Generate dynamic proxies.

public class Person { private String name; public int age; Public void show() {system.out.println (" Hello! The world "); } private String showNation(String nation){system.out.println (" I am: "+ nation); private String showNation(String nation){system.out.println (" I am:" + Nation); return nation; } private Person(String name) { this.name = name; }... } public void test2() throws Exception { Class clazz = Person.class; Constructor cons = clazz.getconstructor (String. Class, int. Class); Object obj = cons.newInstance("Tom", 12); Person p = (Person) obj; System.out.println(p.toString()); Clazz.getdeclaredfield ("age"); clazz.getDeclaredField("age"); age.set(p,10); System.out.println(p.toString()); Clazz.getdeclaredmethod ("show"); clazz.getdeclaredMethod ("show"); method.invoke(p); / / by reflection can be called private properties, methods, constructors / / private Constructor calls the Constructor cons1 = clazz. GetDeclaredConstructor (String. Class); cons1.setAccessible(true); Person p1 = (Person) cons1.newInstance("Jerry"); System.out.println(p1); // Call private method Field name = clazz.getDeclaredField("name"); name.setAccessible(true); name.set(p1,"Han"); System.out.println(p1); Clazz.getdeclaredmethod ("showNation", string.class); method1.setAccessible(true); String invoke = (String) method1. Invoke (p1, "Chinese "); System.out.println(invoke); }./* Welcome to join Java communication Q: 909038429Copy the code

Class loading process: after the program through the java.exe command, will generate one or more bytecode files (.class), then use the java.exe command to interpret the bytecode run. Rather like loading a bytecode file into memory, this process is called class loading. A Class loaded into memory, called a runtime Class, acts as an instance of Class. An instance of Class corresponds to a runtime Class. Runtime classes loaded into memory are cached for a certain amount of time. During this time, we can get the classes from the runtime in different ways.

Public void test3() throws ClassNotFoundException {// Class class clazz1 = person.class; Person = new Person(); Class clazz2 = person.getClass(); Class clazz3 = class.forname (" com.tyt.java.person "); / / way 4: class loader this this this = ReflectionTest. Class. GetClassLoader (); Class clazz4 = classLoader.loadClass("com.tyt.java.Person"); System.out.println(clazz1 == clazz2); System.out.println(clazz2 == clazz3); System.out.println(clazz3 == clazz4); }Copy the code

A Class instance can be a specification of those structures:

public void test5() { Class c1 = Object.class; Class c2 = Comparable.class; Class c3 = String[].class; Class c4 = int[][].class; Class c5 = ElementType.class; Class c6 = Override.class; Class c7 = int.class; Class c8 = void.class; Class c9 = Class.class; int[] a = new int[10]; int[] b = new int[100]; Class c10 = a.getClass(); Class c11 = b.getClass(); // Class system.out.println (c10 == c11) as long as the array element type is the same as the dimension; // true }Copy the code

Use Properties: To read configuration files.

/ / / / | directory structure - the module / / | -- - on STC / / | -- com. Tyt. Java / / | -- -- -- -- - PropertiesTest. Java / / | -- -- -- -- - jdbc1. The properties / / |--jdbc.properties public void test2() { String user = null; String passwored = null; FileInputStream inputStream = null; try { Properties properties = new Properties(); // inputStream = new FileInputStream("jdbc.properties"); // properties.load(inputStream); / / reading configuration way 2: use this / / the default configuration file identified as: the current module under the SRC of this this = ClassLoaderTest. Class. GetClassLoader (); InputStream inputStream1 = classLoader.getResourceAsStream("jdbc1.properties"); properties.load(inputStream1); user = properties.getProperty("user"); passwored = properties.getProperty("passwored"); System.out.println("user = " + user + ",passwored = " + passwored); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream ! = null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); }}}}Copy the code

Mysql, Netty, Spring, thread, Spring Cloud, JVM, source code, algorithm, etc., also have a detailed learning plan map, interview questions, etc., need to obtain these contents of the friend please add Q: sample: 909038429/./* Welcome to Java chat