Home > Java > javaTutorial > body text

Commonly used calling methods in Java reflection

WBOY
Release: 2023-12-23 09:49:05
Original
622 people have browsed it

Commonly used calling methods in Java reflection

Commonly used calling methods in Java reflection require specific code examples

Introduction:
Java reflection is a powerful language feature of Java, which allows us to You can dynamically obtain class information and operate class properties, methods, constructors, etc. at runtime. In Java, by using reflection, we can achieve many functions, such as dynamically creating objects, calling methods, and modifying private properties. This article will introduce the commonly used calling methods in reflection in Java and provide specific code examples.

  1. Get the Class object
    Before using reflection, you first need to get the Class object of the class to be operated on. The Class object can be obtained in the following three ways:
    1.1 Use the getClass() method of the object
    For example, if we have an object named "example", we can use the following code to obtain its Class object:

    Class<?> clazz = example.getClass();
    Copy after login

    1.2 Use .class syntax
    If we use the class name directly to get the Class object, we can use the following syntax:

    Class<?> clazz = Example.class;
    Copy after login

    1.3 Use the Class.forName() method
    If we only Knowing the name of the class, you can use the following code to obtain the Class object:

    Class<?> clazz = Class.forName("com.example.Example");
    Copy after login
  2. Create Object
    Use reflection to dynamically create objects at runtime. An example object can be created by the following code:

    Example example = (Example) clazz.getDeclaredConstructor().newInstance();
    Copy after login

    It is assumed here that Example is the name of the class we want to create.

  3. Get the member variables of the class
    You can use reflection to get the member variables of the class. The following code demonstrates how to obtain the private member variables of the class and modify their values:

    Field field = clazz.getDeclaredField("privateField");
    field.setAccessible(true);
    field.set(example, "new value");
    Copy after login

    In the example, "privateField" is a private member variable of the class Example. First, get the Field object of the member variable by calling the getDeclaredField() method, then set it to be accessible, and finally use the field.set() method to modify its value.

  4. Calling methods
    You can use reflection to call class methods. The following is a sample code that demonstrates how to call the public method of the class:

    Method method = clazz.getMethod("publicMethod", String.class);
    method.invoke(example, "parameter");
    Copy after login

    In the example, "publicMethod" is a public method of the class Example. First, get the Method object of the method by calling the getMethod() method, and then use the method.invoke() method to invoke the method.

  5. Call private methods
    Through reflection, we can also call private methods of the class. The following code demonstrates how to call a private method of a class:

    Method method = clazz.getDeclaredMethod("privateMethod");
    method.setAccessible(true);
    method.invoke(example);
    Copy after login

    In the example, "privateMethod" is a private method of the class Example. First, get the method's Method object by calling the getDeclaredMethod() method, then make it accessible, and finally use the method.invoke() method to invoke the method.

Summary:
Through reflection, we can dynamically obtain class information and operate class attributes, methods, constructors, etc. at runtime. This article introduces the commonly used calling methods in reflection in Java and provides specific code examples. Using reflection can bring great convenience to our development, but excessive use of reflection can also cause problems with code readability and performance, so we need to consider carefully when using reflection.

The above is the detailed content of Commonly used calling methods in Java reflection. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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!