Comparison of three ways to obtain class objects (class objects) in java

The way the Class object is generated is as follows:

Class name.Class
description: JVM will use the class loader to load the class into memory (provided that the class has not been loaded into memory), and does not initialize the class. Return the object of Class

Class.forName ("class name string")
Note: the class name string is the package name + class name
Description: load the class, and do static initialization of the class, and return the object of the Class

Instance object.getClass()
Description: statically initialize and non-statically initialize the class; return the Class object of the class to which the object actually refers to when the reference is returned.
Because: the reference of the child object may be assigned to the reference variable of the parent object

ps:
class object -------- an object representing the class itself,
instance object -------- an object coming out of the class new

public class Person {
    
    
    private String name;
    int age;
    public String address;
    
    public Person() {
    
    
    }
    
    private Person(String name){
    
    
        this.name = name;
    }
    
    Person(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }

    public Person(String name,int age, String address) {
    
    
        this.name = name;
        this.age = age;
        this.address = address;
    }
    
    public void show() {
    
    
        System.out.println("show");
    }

    public void method(String s) {
    
    
        System.out.println("method "+ s);
    }

    public String getString(Strings,inti) {
    
    
        return s +"---" + i;
    }
    
    private void function() {
    
    
        System.out.println("function");
    }
    
    @Override
    public String toString() {
    
    
        return "Person [name="+ name+ ", age="+ age+ ", address=" + address
                +"]";
    }
}



/*
 * 反射:就是通过class文件对象,去使用该文件中的成员变量,构造方法,成员方法。
 *
 * Person p = new Person();
 * p.使用
 *
 * 要想这样使用,首先你必须得到class文件对象,其实也就是得到Class类的对象。
 * Class类:
 *    成员变量  Field
 *    构造方法  Constructor
 *    成员方法  Method
 *
 * 获取class文件对象的方式:
 * A:Object类的getClass()方法
 * B:数据类型的静态属性class
 * C:Class类中的静态方法
 *    public static Class forName(String className)
 *
 * 一般我们到底使用谁呢?
 *    A:自己玩 任选一种,第二种比较方便
 *    B:开发第三种
 *        为什么呢?因为第三种是一个字符串,而不是一个具体的类名。这样我们就可以把这样的字符串配置到配置文件中。
 */

public classReflectDemo {
    
    
    public static void main(String[] args)throws ClassNotFoundException{
    
    
        // 方式1
        Person p = new Person();
        Class c = p.getClass();

        Person p2 = new Person();
        Class c2 = p2.getClass();

        System.out.println(p == p2);// false
        System.out.println(c == c2);// true
        
        // 方式2
        Classc3 = Person.class;
        // int.class;
        // String.class;
        System.out.println(c == c3);
        
        // 方式3
        // ClassNotFoundException
        Class c4 = Class.forName("day27.Person");
        System.out.println(c == c4);
    }
}
        
运行结果:
false
true
true
true

This article reference:
https://blog.csdn.net/wangyanming123/article/details/51355213?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog -OPENSEARCH-2.control

https://blog.csdn.net/cchchunhe/article/details/82022005?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522160810574516780277812872%252522%25252C%252522scm%252522%25253A%25252220140713.130102334.pc%25253A%25252220140713.130102334.pc% 252522%25257D&request_id=160810574516780277812872&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2 all first_rank_v2~rank_v29-1-82022005.nonecase&utm_term=.class%20Get objects

Guess you like

Origin blog.csdn.net/weixin_45024585/article/details/111288322