java中Class对象详解

Class类(在java.lang包中,Instances of the class Classrepresent classes and interfaces in a running Javaapplication):
   在Java中,每个class都有一个相应的Class对象。也就是说,当我们编写一个类,编译完成后,在生成的.class文件中,就会产生一个Class对象,用于表示这个类的类型信息
   获取Class实例的三种方式:
     (1)利用对象调用getClass()方法获取该对象的Class实例;
     (2) 使用Class类的静态方法forName(),用类的名字获取一个Class实例(staticClass forName(String className)  Returns the Classobject associated with the class or interface with the given stringname. );
     (3)运用.class的方式来获取Class实例,对于基本数据类型的封装类,还可以采用.TYPE来获取相对应的基本数据类型的Class实例
   在 newInstance()调用类中缺省的构造方法 ObjectnewInstance()(可在不知该类的名字的时候,常见这个类的实例) Creates a new instance of the class represented by this Classobject.
   在运行期间,如果我们要产生某个类的对象,Java虚拟机(JVM)会检查该类型的Class对象是否已被加载。如果没有被加载,JVM会根据类的名称找到.class文件并加载它。一旦某个类型的Class对象已被加载到内存,就可以用它来产生该类型的所有对象


public static void main(String[] args) {
      
     //获取student类的class对象(每个类都有自己的字节码)
      System.out.println(student.class);
      student t;
    try {

      //用student的class对象实例化student对象
        t = student.class.newInstance();
        System.out.println(t.getId()+t.getName()+t.getAge());
         System.out.println(t.getClass()+":"+t.getClass().getName().substring(18, 24));
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
   

原文地址:https://www.cnblogs.com/CooderIsCool/p/4744074.html