Six ways to obtain class objects

Foreword:
There are three main stages when a Java program runs on a computer, the code stage/compilation stage, the Class stage (loading stage), and the runtime stage. At different stages we can obtain class objects through different methods.
Insert image description here


1. Obtain the instance through the Class class static method forName ("class package name.class name")

The premise of this method is that the full class name of a class is known, and the class is on the class path and can be obtained through the static method forName() of the class class, which may throw classnotfoundException.
Example:

//1 Class.forName
        String classAllPath = "com.test.Car";  //通过读取配置文件获取
        Class<?> cls1 = Class.forName(classAllPath);

Application scenarios: mostly used for configuration files, reading full paths, loading classes


2. Use class name.class to obtain

If the specific class is known, it can be obtained through the class of the class. This method is the safest and most reliable, and has the highest program performance.
Example:

 //2 类名.class,应用场景,有对象实例
        Class cls2 = Car.class;
        System.out.println(cls2);

Application scenarios: Mostly used for parameter passing, such as obtaining the corresponding constructor object through reflection.


3. Obtained through object.getclass()

If an instance of a certain class is known, call the getclass() method of the instance to obtain the class object.
Example:

//3. 对象.getClass(), 应用场景,有对象实例
        Car car = new Car();
        Class cls3 = car.getClass();
        System.out.println(cls3);

Application scenario: Obtain the class object through the created object.


4. Obtain objects through class loaders

To obtain an object through a class loader, you must first obtain the class loader, and then obtain the class object through the class loader.
Example:

//4.通过类加载器来获取类的class对象
           //(1)先获得类加载器 car
        ClassLoader    classLoader = car.getClass().getClassLoader();
           //(2)通过类加载器获取Class对象
        Class cls4 = classLoader.loadClass(classAllPath);
        System.out.println(cls4);


Five basic data types to obtain class objects

Example:

//5.基本数据 (int,char,boolean,float,double,byte,long,short) 按如下方式获得class对象
        Class<Integer> integerClass = int.class;
        Class<Character> characterClass = char.class;
        Class<Boolean> booleanClass = boolean.class;
        System.out.println(integerClass); //int

The packaging classes corresponding to the six basic data types are obtained through .TYPE

Example:

 //6.基本类型对应的包装类,可以通过 .TYPE 得到Class类对象
        Class<Integer> type = Integer.TYPE;
        Class<Character> type1 = Character.TYPE;//其它包装类Boolean,Double,long,byte等等。

Summarize

We print the previous four methods

//cls1,cls2,cls3,cls4 其实是同一个对象
        System.out.println(cls1.hashCode());
        System.out.println(cls2.hashCode());
        System.out.println(cls3.hashCode());
        System.out.println(cls4.hashCode());

result:

Insert image description here


The objects obtained by the four methods are all the same! This is because in the memory, no matter how it is processed, there is only one class object/heap, and for a class, there is only one class object corresponding to it.

Guess you like

Origin blog.csdn.net/a545__27/article/details/130919592