Home > Java > javaTutorial > body text

What are the three methods of java reflection

PHPz
Release: 2023-04-19 21:46:05
forward
5788 people have browsed it

1. Three reflection methods

There are three methods to obtain the Class object of a class: Class.forName(String className), className.class, instance object.getClass().

2. Method description

Reflection mechanism is implemented through new object

Reflection mechanism is implemented through path

Reflection is implemented through class name Mechanism

3, Example

public class Demo(){
F f=new F();
//第一种表达方式
Class c1=F.class;//这种表达方式同时也告诉了我们任何一个类都有一个隐含的静态成员变量class
//第二种表达方式
Class c2=f.getClass();//这种表达方式在已知了该类的对象的情况下通过getClass方法获取
//第三种表达方式
Class c3 = null;
try {
c3 = Class.forName("com.text.F");//类的全称
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
class F{}
Copy after login

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

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!