Get class object in Java

1 Why should we obtain the class object?

When we want to obtain class information and methods, we use the reflection mechanism in Java to facilitate us to write code more flexibly. We can assemble the code when the program is running, and we can also implement dynamic agents.

The reflection mechanism allows the program to obtain the internal information of any class with a known name during runtime.

Then there are three main ways to obtain class objects and content information.

2 Use getClass() method

2.1 Create Person class

package com.clazz;

public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

2.2 Create test class

package com.clazz;

public class Test {
    public static void main(String[] args) {
        Person person = new Person();
        Class clazz = person.getClass();
        System.out.println(clazz.getName());
    }
}

The output result is the complete package path name and class name of the class:

com.clazz.Person

What needs to be noted is: When multiple object instances are created for the same class at the same time, there is actually only one copy of the class object.

Person person1 = new Person();
Person person2 = new Person();
System.out.println(person1.getClass() == person2.getClass());

The result of running is true.

3 Use class static attributes

This method does not require instantiation of the class, and the class object of the class can be obtained directly by using the class static attribute of the class.

package com.clazz;

public class Test {
    public static void main(String[] args) {
        Class<Person> personClass = Person.class;
        System.out.println(personClass.getName());
    }
}

The output result is the complete package path name and class name of the class:

com.clazz.Person

4 Use the static method forName() in the Class class

To obtain the class object in this way, you need to specify the full path of the Class object class to be obtained in the forName() method, that is, the package name + class name.

package com.clazz;

public class Test {
    public static void main(String[] args) {
        try {
            Class<?> aClass = Class.forName("com.clazz.Person");
            System.out.println(aClass.getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

The output result is the complete package path name and class name of the class:

com.clazz.Person

There is a problem that needs attention here. Because we manually specify the full path information of this class, it is possible that the package has not been introduced by us, or the package name and path information are entered incorrectly by hand sliding, and an error may be reported, so this method requires Throw an exception outward.

 

Guess you like

Origin blog.csdn.net/QQ156881887/article/details/129636929