search
HomeJavaJavagetting Startedwhat is java reflection mechanism

what is java reflection mechanism

Jan 19, 2020 pm 05:02 PM
javareflection mechanism

what is java reflection mechanism

java: "Everything is an object", I feel that the Java language itself is constantly practicing this sentence.

The basis of the Java reflection mechanism comes from a Class class. We can read, instantiate, etc. each class through the methods in this class.

Object represents or encapsulates some data. After a class is loaded, jvm will create a Class object corresponding to the class (a class will only correspond to one Class object), and the entire structural information of the class will be placed in the corresponding Class object. This Class object is like a mirror, through which you can see all the information of the corresponding class.

Free learning video tutorial sharing: java video tutorial

For example:

The instantiation process of a Person:

Person person = new Person();

Apply reflection to achieve:

Class clz = null;
String name = "com.zl.server.Person";   //类的地址
try {
    clz = Class.forName(name);   //将类的地址传入
    Person person = (Person)clz.getConstructor().newInstance();    //获取构造器,进行实例化
    return servlet;
} catch (Exception e) {
    e.printStackTrace();
}

The reflection mechanism will make the instantiation of classes more flexible.

In many frameworks, reflection has been widely used. We can obtain the annotations, constructors, properties, etc. of the class through Class to perform more processing.

Recommended related articles and tutorials: java introductory tutorial

The above is the detailed content of what is java reflection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)