Learn Java reflection with me--one step

Source: Internet
Author: User

What is reflection?

Reflection (reflection) is Java The program can dynamically get internal information of all classes inside the program at run time, and can dynamically invoke the internal properties and methods of any object.

Why reflection is needed

Why we want to use reflection, which is mainly the dynamic nature of reflection, because reflection can achieve dynamic creation of objects, which greatly played a Java flexibility to reduce the coupling of program calls, so that the system can be more flexible and can better deal with the change.

Reflective Applications


running Reflection We can do:

Determine the class to which any object belongs at run time

To construct an object of any class at run time

Determine the member variables and methods that any one class has at run time

To invoke member variables and methods of any object at run time

Generating dynamic agents

The common classes associated with reflection are:

Java.lang.Class: Represents a class

Java.lang.reflect.Method: Methods for representing classes

Java.lang.reflect.Field: A member variable representing a class

Java.lang.reflect.Constructor: The construction method of the Representative class


class

Class is the source of the reflection mechanism, in fact the so-called reflection from the running results of the program is to get the name of the class through object reflection.

Reflection is actually the inverse of the normal way of using a class like a reflection in physics:


by reflecting the information we can get: the properties of a class, methods, constructors, which interfaces the class implements, and what the parent class is. For each class,theJRE retains an object of the same class type for which a class object contains information about a particular class.

Class itself is also a category in which each class JVM there is only one class instance in each class object that corresponds to a loaded JVM in one of . Class files, each instance of the class will remember which class instance it was generated from.


Four ways to get class objects by reflection

we now have a well-established class Person , the classpath is the package com.tgb.reflect.common;

1. The specific person class is known through the class Property, the method is most safe and reliable, and the program performance is highest.

@Testpublic void Test1 () {Classclass1 = Person.class; System.out.println (class1);}


2. If an instance of the person class is known , call the instance's getclass() method to get the class Object

@Testpublic void Test2 () {personp = new person (); Classclass1 =p.getclass (); System.out.println (class1);}


3. the full class name of the person class is known, and the class is under the Classpath by using The class's static method for Name () gets.

@Testpublic void Test3 () throws Classnotfoundexception{stringclassname = "Com.tgb.reflect.common.Person"; Classclass1 = Class.forName (className); System.out.println (class1);}


4. get The class object through the ClassLoader.

@Testpublic void Test4 () throws Classnotfoundexception{stringclassname= "Com.tgb.reflect.common.Person"; Classloaderclassloader =this.getclass (). getClassLoader (); Classclass1=classloader.loadclass (ClassName); System.out.println (class1);}


The above four ways to get the Class object code to run the result is the same, run the result is " Classcom.tgb.reflect.common.Person";  

class Loader

when our program actively uses a class, if the class has not yet been loaded into memory, the system initializes the class with the following three steps.


the ClassLoader (class loader) is used to put the class ( . Class When loaded into memory, the JVM generates an initializer hierarchy of 3 ClassLoader at run time, as shown in:


Let's look at the code:

@Testpublic void Test1 () throws classnotfoundexception{//1. Gets a System class loader ClassLoaderloader1  = Classloader.getsystemclassloader (); SYSTEM.OUT.PRINTLN ("System class Loader:" +loader1); 2. Get the parent class loader of the system ClassLoader, that is, the extension classloader ClassLoaderloader2 = Loader1.getparent (); System.out.println ("Extension class loader:" +loader2); 3. Gets the extension class loader's parent ClassLoader, which is the boot classloader ClassLoaderloader3 = loader2.getparent (); SYSTEM.OUT.PRINTLN ("Boot class loader:" +loader3); 4. Test which class loader is loading the current class Classclass1 = Person.class; Classloaderloader4= Class1.getclassloader (); SYSTEM.OUT.PRINTLN ("class loader for the current class:" +loader4); 5. Test which class loader the String class provided by the JDK loads stringclassname= "java.lang.String"; Classclass2 = Class.forName (className); Classloaderloader5= Class2.getclassloader (); System.out.println ("string ClassLoader provided by JDK:" +loader5);}


run the result as :


The System class loader is: [email protected]

Extension class loader: [email protected]

Boot class loader: null

Class loader for the current class: [Email protected]

String class loader provided by JDK: null

The result of the code run we can see that our own class is loaded by the System class loader, the System class loader on the level of the Extension class loader, the extension class loader on the first level of the system does not allow us to get, but the expansion class loader is really the boot class loader, such loaders are mainly used to load The class library that the JVM comes with.

About the ClassLoader sometimes we can use to read the configuration file in the system, here we have a simple small example, we put a test.properties file into our class directory, we use the ClassLoader to read the contents of it.

The content of our configuration file is simple:


User=root

password=123456

The code is as follows:



The operating result is:


User:root

password:123456


Postscript


This article introduces the basics of reflection and the source of reflection . Class class, and tells about getting Class class object in four ways, but also introduced the class loader some knowledge, about reflection this article first introduced so much, the next article we continue.

Learn Java reflection with me--one step

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.