原视频地址: 【狂神说Java】注解和反射,强烈推荐大家学习

什么是注解

什么是注解?

  • Annotation是从JDK5.0开始引入的新技术.

  • Annotation的作用:

    • 不是程序本身,可以对程序作出解释.(这一点和注释(comment)没什么区别)
    • 可以被其他程序(比如:编译器等)读取.
      • 通过反射获取
  • Annotation的格式:

    • 注解是以“@注释名“在代码中存在的,还可以添加一些参数值,例如:@SuppressWarnings(value=“unchecked”).
  • Annotation在哪里使用?
    可以附加在package,class,method,field等上面,相当于给他们添加了额外的辅助信息
    我们可以通过反射机制编程实现对这些元数据的访问

    • 注解还有其规范以及约束的作用

      @Override  //正常
      public String toString(){return  null;
      }@Override  //报错:Method does not override method from its superclas
      public String tostring(){return  null;
      }
      

内置注解

常见的三个内置注解:

  • @Override:定义在java.lang.Override中,此注释只适用于修辞方法,表示一个方法声明打算重写超类中的另一个方法声明

  • 定义

    @Target(value=METHOD)@Retention(value=SOURCE)
    public @interface Override
    

    表示方法声明旨在覆盖超类型中的方法声明。 如果使用此注释类型注释方法,则除非至少满足以下条件之一,否则需要编译器生成错误消息:

    • 该方法将覆盖或实现在超类型中声明的方法。
    • 该方法具有与Object中声明的任何公共方法的覆盖相同的签名 。
  • @Deprecated:定义在java.lang.Deprecated中,此注释可以用于修辞方法,属性,类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或者存在更好的选择.

    • 不推荐是使用,但是还是可以使用
  • @SuppressWarnings:定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息.

    • 定义

      @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
      @Retention(RetentionPolicy.SOURCE)
      public @interface SuppressWarnings {String[] value;
      }
      
    • 与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好了的,我们选择性的使用就好了:

      @SuppressWarnings("all")@SuppressWarnings("unchecked")@SuppressWarnings(value=({"unchecked","deprecation"})
      

元注解

  • 元注解的作用就是负责注解其他注解(也就是可以定义其他注解的注解禁止套娃)其本质上也还是注解,Java定义了4个标准的meta-annotation类型,他们被用来提供对其他annotation类型作说明.

  • 这些类型和它们所支持的类在java.lang.annotation包中可以找到.

    @Target,@Retention,@Documented,@Inherited
    
    • @Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

      @Documented
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.ANNOTATION_TYPE)
      public @interface Target {ElementType[] value();
      }
      
      public enum ElementType {/** Class, interface (including annotation type), or enum declaration */TYPE,/** Field declaration (includes enum constants) */FIELD,/** Method declaration */METHOD,/** Formal parameter declaration */PARAMETER,/** Constructor declaration */CONSTRUCTOR,/** Local variable declaration */LOCAL_VARIABLE,/** Annotation type declaration */ANNOTATION_TYPE,/** Package declaration */PACKAGE,/*** Type parameter declaration* @since 1.8*/TYPE_PARAMETER,/*** Use of a type* @since 1.8*/TYPE_USE
      }
      
    • @Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(SOURCE < CLASS < RUNTIME)

      @Documented
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.ANNOTATION_TYPE)
      public @interface Retention {RetentionPolicy value();
      }
      
      public enum RetentionPolicy {/*** Annotations are to be discarded by the compiler.*/SOURCE,/*** Annotations are to be recorded in the class file by the compiler* but need not be retained by the VM at run time.  This is the default behavior.*/CLASS,/*** Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.**/RUNTIME
      }
      
    • @Document:说明该注解将被包含在javadoc中

      表示是否将我们的注解生成在Javadoc文档中

      @Documented
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.ANNOTATION_TYPE)
      public @interface Documented {}
      
    • @Inherited:说明子类可以继承父类中的该注解

      @Documented
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.ANNOTATION_TYPE)
      public @interface Inherited {}
      

自定义注解

  • 使用**@interface**自定义注解时,自动继承了java.lang.annotation.Annotation接口

  • 分析:

    • @interface用来声明一个注解,

      格式: public@interface注解名{定义内容},其中的每一个方法实际上是声明了一个配置参数.

    • 方法的名称就是参数的名称.

    • 返回值类型就是参数的类型(返回值只能是基本类型,Class,String,enum).

    • 可以通过default来声明参数的默认值

    • 如果只有一个参数成员,一般参数名为value

    • 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值.

  • 代码实例分析:

    //自定义注解
    public class Test03 {//注解可以显示赋值,如无赋值则显示默认值//赋值没有顺序@MyAnnotation2(schools = "清华大学",id = 99)public void testAnno(){};//若注解只有一个名为vaule的参数,可以省略参数名,直接赋值即可@MyAnnotation3("Wayne")public void testAnno2(){};}@Target({ElementType.TYPE,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @interface MyAnnotation2{//注解的参数表示: 参数类型 + 参数名称();//如有default 默认值则可以不用赋值,如无传值则会报错String name () default  "";int age() default 0;int id () default -1;String[] schools ()  default {"广东工业大学"};
    }@Target({ElementType.TYPE,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @interface MyAnnotation3{//注解的参数表示: 参数类型 + 参数名称();String value();//如String name(); 注解必须写上参数名
    }
    

反射机制:

几乎所有框架都是用反射实现动态代理,毫不过分地说:反射是框架的灵魂 因此学习反射非常重要。

面试可以吹这个点

前置知识:

静态 VS 动态语言

  • 动态语言

    • 是一类在运行时可以改变其结构的语言:例如新的函数、对象、甚至代码可以被引进,已有的函数可以被删除或是其他结构上的变化。

      • 通俗点说就是在运行时代码可以根据某些条件改变自身结构。
    • 主要动态语言:Object-C、C#、JavaScript、PHP、Python等。

      var x = "var a=3;var b=5;alert(a+b)";
      eval(x);//最后弹窗显示: 8
      //将原本字符串的a、b进行了转换,加以运算得出的
      
  • 静态语言

    • 与动态语言相对应的,运行时结构不可变的语言就是静态语言。如Java、C、C++。
    • Java不是动态语言,但Java可以称之为“准动态语言”。即Java有一定的动态性,我们可以利用反射机制获得类似动态语言的特性。Java的动态性让编程的时候更加灵活!

Java反射概述

  • Reflection(反射)是Java被视为动态语言的关键,反射机制允许程序在执行期借
    助于Reflection API 取得任何类的内部信息,并能直接操作任意对象的内部属性及
    方法。

    • 内部信息:类名、类方法、类属性等…
    Class c = Class.forName("java.lang.String")
    
  • 加载完类之后,在堆内存的方法区中就产生了一个Class类型的对象。

    (一个类只有一个Class对象,可以简单理解为“模板”,可以根据这个模板实例化对象)

    这个对象就包含了完整的类的结构信息。我们可以通过这个对象看到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以,我们形象的称之为:反射

    • 正常方式:

      引入需要的“包类”名称 --> 通过new实例化 --> 取得实例化对象

    • 反射方式:

      实例化对象 --> getClass()方法 -->得到完整的“包类”名称

获得反射对象

Java反射机制研究及应用

Java反射机制提供的功能:

  • 在运行时判断任意一个对象所属的类
  • 在运行时构造任意一个类的对象
  • 在运行时判断任意一个类所具有的成员变量和方法
  • 在运行时获取泛型信息
  • 在运行时调用任意一个对象的成员变量和方法
  • 在运行时处理注解
  • 生成动态代理

优点:

  • 可以实现动态创建对象和编译,体现出很大的灵活性

缺点:

  • 对性能有影响。使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望做什么并且它满足我们的要求。这类操作总是慢于直接执行相同的操作。

Class类:

在Object类中定义了以下的方法,此方法将被所有子类继承

public final Class getClass()
  • 以上的方法返回值的类型是一个Class类,此类是Java反射的源头,实际上所
    谓反射从程序的运行结果来看也很好理解,即:可以通过对象反射求出类的名
    称。
  • 对象照镜子后可以得到的信息:
    • 类的属性
    • 方法
    • 构造器
    • 某个类到底实现了哪些接口
  • 对于每个类而言,JRE都为其保留一个不变的Class类型的对象。一个Class对象包含了特定某个结构(class/interface/enum/annotation/primitive type/void/])的有关信息。
    • Class本身也是一个类
    • Class对象只能由系统建立对象
    • 一个加载的类在JVM中只会有一个Class实例 (只有一个模板
    • 一个Class对象对应的是一个加载到JVM中的一个.class文件
    • 每个类的实例都会记得自己是由哪个Class实例所生成
    • 通过Class可以完整地得到一个类中的所有被加载的结构
    • Class类是Reflection的根源,针对任何你想动态加载、运行的类,唯有先获得相应的Class对象

得到Class类的几种方式

Class类的常用方法

方法名 功能说明
static ClassforName(String name) 返回指定类名name的Class对象
Object newlnstance() 调用缺省构造函数,返回Class对象的一个实例
getName() 返回此Class对象所表示的实体(类,接口,数组类或void)的名称。
Class getSuperClass() 返回当前Class对象的父类的Class对象
Class[] getinterfaces() 获取当前Class对象的接口
ClassLoader getClassLoader() 返回该类的类加载器
Constructor[] getConstructors() 返回一个包含某些Constructor对象的数组
Method getMothed(String name,Class…T) 返回一个Method对象,此对象的形参类型为paramType
Field[] getDeclaredFields() 返回Field对象的一个数组

获取Class类的实例

  1. 若已知具体的类,通过类的class属性获取,该方法最为安全可靠,程序性能最高。

    Class clazz = Person.class;
    
  2. 已知某个类的实例,调用该实例的getClass()方法获取Class对象

Class clazz=person.getClass();
  1. 已知一个类的全类名,且该类在类路径下,可通过Class类的静态方法forName()获取可能抛出ClassNotFoundException

    Class clazz = Class.forName("demo01.Student");
    
  2. 内置基本数据类型可以直接用类名.Type

  3. 还可以利用ClassLoader我们之后讲解

public class TestReflect02 {public static void main(String[] args) throws ClassNotFoundException {Person person = new Student();System.out.println("这个人是:"+person.name);//这个人是:学生//方式1:通过class属性Class cl1 = Student.class;System.out.println(cl1.hashCode());  //312714112//方式2:通过getClass方法Class cl2=person.getClass();System.out.println(cl2.hashCode());//312714112//方式3:Class cl3 = Class.forName("com.kuangstudy.AnnotationAndReflect.Student");System.out.println(cl3.hashCode());//312714112//4.内置基本数据类型可以直接用类名.TypeClass cl4 = Integer.TYPE ;System.out.println(cl4);//int//5.获取父类:Class c5 = cl1.getSuperclass();System.out.println(c5);//class com.kuangstudy.AnnotationAndReflect.Person}}class Person{String name;public Person() {}public Person(String name) {this.name = name;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +'}';}
}class Teacher extends Person{public Teacher() {this.name = "教师";}
}class Student extends Person{public Student() {this.name = "学生";}
}

所有类型的Class对象

哪些类型可以有Class对象?

  • class:外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类。
  • interface:接口
  • [] :数组
  • enum:枚举
  • annotation:注解@interface
  • primitive type:基本数据类型
  • void
public class Test04 {//所有类型的classpublic static void main(String[] args) {Class c1 = Object.class ; //类Class c2 = Comparable.class;//接口Class c3 = String[].class;//一维数组Class c4 = int[][].class;//二维数组Class c5 = Override.class ; //注解Class c6 = ElementType.class;//枚举Class c7 = Integer.class;//基本数据类型Class c8 = void.class ; //voidClass c9 = Class.class ; // classSystem.out.println(c1);// class java.lang.ObjectSystem.out.println(c2);// interface java.lang.ComparableSystem.out.println(c3);// class [Ljava.lang.String;System.out.println(c4);// class [[ISystem.out.println(c5);// interface java.lang.OverrideSystem.out.println(c6);// class java.lang.annotation.ElementTypeSystem.out.println(c7);// class java.lang.IntegerSystem.out.println(c8);// voidSystem.out.println(c9);// class java.lang.Class//只要元素类型与维度一样,就是同一个Classint[] a = new int[10];int[] b = new int[100];System.out.println(a.getClass().hashCode());//312714112System.out.println(b.getClass().hashCode());//312714112}
}

类加载内存分析

Java内存分析:

#mermaid-svg-ATwZvkowKLmW8gOi .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-ATwZvkowKLmW8gOi .label text{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .node rect,#mermaid-svg-ATwZvkowKLmW8gOi .node circle,#mermaid-svg-ATwZvkowKLmW8gOi .node ellipse,#mermaid-svg-ATwZvkowKLmW8gOi .node polygon,#mermaid-svg-ATwZvkowKLmW8gOi .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ATwZvkowKLmW8gOi .node .label{text-align:center;fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .node.clickable{cursor:pointer}#mermaid-svg-ATwZvkowKLmW8gOi .arrowheadPath{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-ATwZvkowKLmW8gOi .flowchart-link{stroke:#333;fill:none}#mermaid-svg-ATwZvkowKLmW8gOi .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-ATwZvkowKLmW8gOi .edgeLabel rect{opacity:0.9}#mermaid-svg-ATwZvkowKLmW8gOi .edgeLabel span{color:#333}#mermaid-svg-ATwZvkowKLmW8gOi .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-ATwZvkowKLmW8gOi .cluster text{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-ATwZvkowKLmW8gOi .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ATwZvkowKLmW8gOi text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-ATwZvkowKLmW8gOi .actor-line{stroke:grey}#mermaid-svg-ATwZvkowKLmW8gOi .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-ATwZvkowKLmW8gOi .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-ATwZvkowKLmW8gOi #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-ATwZvkowKLmW8gOi .sequenceNumber{fill:#fff}#mermaid-svg-ATwZvkowKLmW8gOi #sequencenumber{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi #crosshead path{fill:#333;stroke:#333}#mermaid-svg-ATwZvkowKLmW8gOi .messageText{fill:#333;stroke:#333}#mermaid-svg-ATwZvkowKLmW8gOi .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ATwZvkowKLmW8gOi .labelText,#mermaid-svg-ATwZvkowKLmW8gOi .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-ATwZvkowKLmW8gOi .loopText,#mermaid-svg-ATwZvkowKLmW8gOi .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-ATwZvkowKLmW8gOi .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-ATwZvkowKLmW8gOi .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ATwZvkowKLmW8gOi .noteText,#mermaid-svg-ATwZvkowKLmW8gOi .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-ATwZvkowKLmW8gOi .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-ATwZvkowKLmW8gOi .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-ATwZvkowKLmW8gOi .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-ATwZvkowKLmW8gOi .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi .section{stroke:none;opacity:0.2}#mermaid-svg-ATwZvkowKLmW8gOi .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-ATwZvkowKLmW8gOi .section2{fill:#fff400}#mermaid-svg-ATwZvkowKLmW8gOi .section1,#mermaid-svg-ATwZvkowKLmW8gOi .section3{fill:#fff;opacity:0.2}#mermaid-svg-ATwZvkowKLmW8gOi .sectionTitle0{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .sectionTitle1{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .sectionTitle2{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .sectionTitle3{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-ATwZvkowKLmW8gOi .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi .grid path{stroke-width:0}#mermaid-svg-ATwZvkowKLmW8gOi .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-ATwZvkowKLmW8gOi .task{stroke-width:2}#mermaid-svg-ATwZvkowKLmW8gOi .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi .taskText:not([font-size]){font-size:11px}#mermaid-svg-ATwZvkowKLmW8gOi .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-ATwZvkowKLmW8gOi .task.clickable{cursor:pointer}#mermaid-svg-ATwZvkowKLmW8gOi .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ATwZvkowKLmW8gOi .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ATwZvkowKLmW8gOi .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ATwZvkowKLmW8gOi .taskText0,#mermaid-svg-ATwZvkowKLmW8gOi .taskText1,#mermaid-svg-ATwZvkowKLmW8gOi .taskText2,#mermaid-svg-ATwZvkowKLmW8gOi .taskText3{fill:#fff}#mermaid-svg-ATwZvkowKLmW8gOi .task0,#mermaid-svg-ATwZvkowKLmW8gOi .task1,#mermaid-svg-ATwZvkowKLmW8gOi .task2,#mermaid-svg-ATwZvkowKLmW8gOi .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-ATwZvkowKLmW8gOi .taskTextOutside0,#mermaid-svg-ATwZvkowKLmW8gOi .taskTextOutside2{fill:#000}#mermaid-svg-ATwZvkowKLmW8gOi .taskTextOutside1,#mermaid-svg-ATwZvkowKLmW8gOi .taskTextOutside3{fill:#000}#mermaid-svg-ATwZvkowKLmW8gOi .active0,#mermaid-svg-ATwZvkowKLmW8gOi .active1,#mermaid-svg-ATwZvkowKLmW8gOi .active2,#mermaid-svg-ATwZvkowKLmW8gOi .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-ATwZvkowKLmW8gOi .activeText0,#mermaid-svg-ATwZvkowKLmW8gOi .activeText1,#mermaid-svg-ATwZvkowKLmW8gOi .activeText2,#mermaid-svg-ATwZvkowKLmW8gOi .activeText3{fill:#000 !important}#mermaid-svg-ATwZvkowKLmW8gOi .done0,#mermaid-svg-ATwZvkowKLmW8gOi .done1,#mermaid-svg-ATwZvkowKLmW8gOi .done2,#mermaid-svg-ATwZvkowKLmW8gOi .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-ATwZvkowKLmW8gOi .doneText0,#mermaid-svg-ATwZvkowKLmW8gOi .doneText1,#mermaid-svg-ATwZvkowKLmW8gOi .doneText2,#mermaid-svg-ATwZvkowKLmW8gOi .doneText3{fill:#000 !important}#mermaid-svg-ATwZvkowKLmW8gOi .crit0,#mermaid-svg-ATwZvkowKLmW8gOi .crit1,#mermaid-svg-ATwZvkowKLmW8gOi .crit2,#mermaid-svg-ATwZvkowKLmW8gOi .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-ATwZvkowKLmW8gOi .activeCrit0,#mermaid-svg-ATwZvkowKLmW8gOi .activeCrit1,#mermaid-svg-ATwZvkowKLmW8gOi .activeCrit2,#mermaid-svg-ATwZvkowKLmW8gOi .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-ATwZvkowKLmW8gOi .doneCrit0,#mermaid-svg-ATwZvkowKLmW8gOi .doneCrit1,#mermaid-svg-ATwZvkowKLmW8gOi .doneCrit2,#mermaid-svg-ATwZvkowKLmW8gOi .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-ATwZvkowKLmW8gOi .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-ATwZvkowKLmW8gOi .milestoneText{font-style:italic}#mermaid-svg-ATwZvkowKLmW8gOi .doneCritText0,#mermaid-svg-ATwZvkowKLmW8gOi .doneCritText1,#mermaid-svg-ATwZvkowKLmW8gOi .doneCritText2,#mermaid-svg-ATwZvkowKLmW8gOi .doneCritText3{fill:#000 !important}#mermaid-svg-ATwZvkowKLmW8gOi .activeCritText0,#mermaid-svg-ATwZvkowKLmW8gOi .activeCritText1,#mermaid-svg-ATwZvkowKLmW8gOi .activeCritText2,#mermaid-svg-ATwZvkowKLmW8gOi .activeCritText3{fill:#000 !important}#mermaid-svg-ATwZvkowKLmW8gOi .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-ATwZvkowKLmW8gOi g.classGroup text .title{font-weight:bolder}#mermaid-svg-ATwZvkowKLmW8gOi g.clickable{cursor:pointer}#mermaid-svg-ATwZvkowKLmW8gOi g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ATwZvkowKLmW8gOi g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-ATwZvkowKLmW8gOi .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-ATwZvkowKLmW8gOi .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ATwZvkowKLmW8gOi .dashed-line{stroke-dasharray:3}#mermaid-svg-ATwZvkowKLmW8gOi #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi .commit-id,#mermaid-svg-ATwZvkowKLmW8gOi .commit-msg,#mermaid-svg-ATwZvkowKLmW8gOi .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-ATwZvkowKLmW8gOi g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-ATwZvkowKLmW8gOi g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ATwZvkowKLmW8gOi g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ATwZvkowKLmW8gOi .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ATwZvkowKLmW8gOi .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-ATwZvkowKLmW8gOi .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-ATwZvkowKLmW8gOi .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ATwZvkowKLmW8gOi .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-ATwZvkowKLmW8gOi .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-ATwZvkowKLmW8gOi .edgeLabel text{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ATwZvkowKLmW8gOi .node circle.state-start{fill:black;stroke:black}#mermaid-svg-ATwZvkowKLmW8gOi .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-ATwZvkowKLmW8gOi #statediagram-barbEnd{fill:#9370db}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-state .divider{stroke:#9370db}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-ATwZvkowKLmW8gOi .note-edge{stroke-dasharray:5}#mermaid-svg-ATwZvkowKLmW8gOi .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-ATwZvkowKLmW8gOi .error-icon{fill:#522}#mermaid-svg-ATwZvkowKLmW8gOi .error-text{fill:#522;stroke:#522}#mermaid-svg-ATwZvkowKLmW8gOi .edge-thickness-normal{stroke-width:2px}#mermaid-svg-ATwZvkowKLmW8gOi .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-ATwZvkowKLmW8gOi .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-ATwZvkowKLmW8gOi .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-ATwZvkowKLmW8gOi .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-ATwZvkowKLmW8gOi .marker{fill:#333}#mermaid-svg-ATwZvkowKLmW8gOi .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-ATwZvkowKLmW8gOi {color: rgba(0, 0, 0, 0.75);font: ;}

Java内存
方法区
存放new的对象和数组
堆可以被所有的线程共享,不会存放别的对象引用
存放基本变量类型:会包含这个基本类型的具体数值
引用对象的变量:会存放这个引用在堆里面的具体地址
可以被所有的线程共享
包含了所有的clazz和static变量

类的加载过程

当程序主动使用某个类时,如果该类还未被加载到内存中,则系统会通过如下三个步骤来对该类进行初始化。

#mermaid-svg-7kUQqS3XueJVya5g .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-7kUQqS3XueJVya5g .label text{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .node rect,#mermaid-svg-7kUQqS3XueJVya5g .node circle,#mermaid-svg-7kUQqS3XueJVya5g .node ellipse,#mermaid-svg-7kUQqS3XueJVya5g .node polygon,#mermaid-svg-7kUQqS3XueJVya5g .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-7kUQqS3XueJVya5g .node .label{text-align:center;fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .node.clickable{cursor:pointer}#mermaid-svg-7kUQqS3XueJVya5g .arrowheadPath{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-7kUQqS3XueJVya5g .flowchart-link{stroke:#333;fill:none}#mermaid-svg-7kUQqS3XueJVya5g .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-7kUQqS3XueJVya5g .edgeLabel rect{opacity:0.9}#mermaid-svg-7kUQqS3XueJVya5g .edgeLabel span{color:#333}#mermaid-svg-7kUQqS3XueJVya5g .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-7kUQqS3XueJVya5g .cluster text{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-7kUQqS3XueJVya5g .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-7kUQqS3XueJVya5g text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-7kUQqS3XueJVya5g .actor-line{stroke:grey}#mermaid-svg-7kUQqS3XueJVya5g .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-7kUQqS3XueJVya5g .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-7kUQqS3XueJVya5g #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-7kUQqS3XueJVya5g .sequenceNumber{fill:#fff}#mermaid-svg-7kUQqS3XueJVya5g #sequencenumber{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g #crosshead path{fill:#333;stroke:#333}#mermaid-svg-7kUQqS3XueJVya5g .messageText{fill:#333;stroke:#333}#mermaid-svg-7kUQqS3XueJVya5g .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-7kUQqS3XueJVya5g .labelText,#mermaid-svg-7kUQqS3XueJVya5g .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-7kUQqS3XueJVya5g .loopText,#mermaid-svg-7kUQqS3XueJVya5g .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-7kUQqS3XueJVya5g .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-7kUQqS3XueJVya5g .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-7kUQqS3XueJVya5g .noteText,#mermaid-svg-7kUQqS3XueJVya5g .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-7kUQqS3XueJVya5g .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-7kUQqS3XueJVya5g .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-7kUQqS3XueJVya5g .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-7kUQqS3XueJVya5g .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g .section{stroke:none;opacity:0.2}#mermaid-svg-7kUQqS3XueJVya5g .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-7kUQqS3XueJVya5g .section2{fill:#fff400}#mermaid-svg-7kUQqS3XueJVya5g .section1,#mermaid-svg-7kUQqS3XueJVya5g .section3{fill:#fff;opacity:0.2}#mermaid-svg-7kUQqS3XueJVya5g .sectionTitle0{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .sectionTitle1{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .sectionTitle2{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .sectionTitle3{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-7kUQqS3XueJVya5g .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g .grid path{stroke-width:0}#mermaid-svg-7kUQqS3XueJVya5g .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-7kUQqS3XueJVya5g .task{stroke-width:2}#mermaid-svg-7kUQqS3XueJVya5g .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g .taskText:not([font-size]){font-size:11px}#mermaid-svg-7kUQqS3XueJVya5g .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-7kUQqS3XueJVya5g .task.clickable{cursor:pointer}#mermaid-svg-7kUQqS3XueJVya5g .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-7kUQqS3XueJVya5g .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-7kUQqS3XueJVya5g .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-7kUQqS3XueJVya5g .taskText0,#mermaid-svg-7kUQqS3XueJVya5g .taskText1,#mermaid-svg-7kUQqS3XueJVya5g .taskText2,#mermaid-svg-7kUQqS3XueJVya5g .taskText3{fill:#fff}#mermaid-svg-7kUQqS3XueJVya5g .task0,#mermaid-svg-7kUQqS3XueJVya5g .task1,#mermaid-svg-7kUQqS3XueJVya5g .task2,#mermaid-svg-7kUQqS3XueJVya5g .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-7kUQqS3XueJVya5g .taskTextOutside0,#mermaid-svg-7kUQqS3XueJVya5g .taskTextOutside2{fill:#000}#mermaid-svg-7kUQqS3XueJVya5g .taskTextOutside1,#mermaid-svg-7kUQqS3XueJVya5g .taskTextOutside3{fill:#000}#mermaid-svg-7kUQqS3XueJVya5g .active0,#mermaid-svg-7kUQqS3XueJVya5g .active1,#mermaid-svg-7kUQqS3XueJVya5g .active2,#mermaid-svg-7kUQqS3XueJVya5g .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-7kUQqS3XueJVya5g .activeText0,#mermaid-svg-7kUQqS3XueJVya5g .activeText1,#mermaid-svg-7kUQqS3XueJVya5g .activeText2,#mermaid-svg-7kUQqS3XueJVya5g .activeText3{fill:#000 !important}#mermaid-svg-7kUQqS3XueJVya5g .done0,#mermaid-svg-7kUQqS3XueJVya5g .done1,#mermaid-svg-7kUQqS3XueJVya5g .done2,#mermaid-svg-7kUQqS3XueJVya5g .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-7kUQqS3XueJVya5g .doneText0,#mermaid-svg-7kUQqS3XueJVya5g .doneText1,#mermaid-svg-7kUQqS3XueJVya5g .doneText2,#mermaid-svg-7kUQqS3XueJVya5g .doneText3{fill:#000 !important}#mermaid-svg-7kUQqS3XueJVya5g .crit0,#mermaid-svg-7kUQqS3XueJVya5g .crit1,#mermaid-svg-7kUQqS3XueJVya5g .crit2,#mermaid-svg-7kUQqS3XueJVya5g .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-7kUQqS3XueJVya5g .activeCrit0,#mermaid-svg-7kUQqS3XueJVya5g .activeCrit1,#mermaid-svg-7kUQqS3XueJVya5g .activeCrit2,#mermaid-svg-7kUQqS3XueJVya5g .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-7kUQqS3XueJVya5g .doneCrit0,#mermaid-svg-7kUQqS3XueJVya5g .doneCrit1,#mermaid-svg-7kUQqS3XueJVya5g .doneCrit2,#mermaid-svg-7kUQqS3XueJVya5g .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-7kUQqS3XueJVya5g .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-7kUQqS3XueJVya5g .milestoneText{font-style:italic}#mermaid-svg-7kUQqS3XueJVya5g .doneCritText0,#mermaid-svg-7kUQqS3XueJVya5g .doneCritText1,#mermaid-svg-7kUQqS3XueJVya5g .doneCritText2,#mermaid-svg-7kUQqS3XueJVya5g .doneCritText3{fill:#000 !important}#mermaid-svg-7kUQqS3XueJVya5g .activeCritText0,#mermaid-svg-7kUQqS3XueJVya5g .activeCritText1,#mermaid-svg-7kUQqS3XueJVya5g .activeCritText2,#mermaid-svg-7kUQqS3XueJVya5g .activeCritText3{fill:#000 !important}#mermaid-svg-7kUQqS3XueJVya5g .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-7kUQqS3XueJVya5g g.classGroup text .title{font-weight:bolder}#mermaid-svg-7kUQqS3XueJVya5g g.clickable{cursor:pointer}#mermaid-svg-7kUQqS3XueJVya5g g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-7kUQqS3XueJVya5g g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-7kUQqS3XueJVya5g .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-7kUQqS3XueJVya5g .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-7kUQqS3XueJVya5g .dashed-line{stroke-dasharray:3}#mermaid-svg-7kUQqS3XueJVya5g #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g .commit-id,#mermaid-svg-7kUQqS3XueJVya5g .commit-msg,#mermaid-svg-7kUQqS3XueJVya5g .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-7kUQqS3XueJVya5g g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-7kUQqS3XueJVya5g g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-7kUQqS3XueJVya5g g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-7kUQqS3XueJVya5g .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-7kUQqS3XueJVya5g .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-7kUQqS3XueJVya5g .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-7kUQqS3XueJVya5g .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-7kUQqS3XueJVya5g .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-7kUQqS3XueJVya5g .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-7kUQqS3XueJVya5g .edgeLabel text{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7kUQqS3XueJVya5g .node circle.state-start{fill:black;stroke:black}#mermaid-svg-7kUQqS3XueJVya5g .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-7kUQqS3XueJVya5g #statediagram-barbEnd{fill:#9370db}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-state .divider{stroke:#9370db}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-7kUQqS3XueJVya5g .note-edge{stroke-dasharray:5}#mermaid-svg-7kUQqS3XueJVya5g .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-7kUQqS3XueJVya5g .error-icon{fill:#522}#mermaid-svg-7kUQqS3XueJVya5g .error-text{fill:#522;stroke:#522}#mermaid-svg-7kUQqS3XueJVya5g .edge-thickness-normal{stroke-width:2px}#mermaid-svg-7kUQqS3XueJVya5g .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-7kUQqS3XueJVya5g .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-7kUQqS3XueJVya5g .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-7kUQqS3XueJVya5g .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-7kUQqS3XueJVya5g .marker{fill:#333}#mermaid-svg-7kUQqS3XueJVya5g .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-7kUQqS3XueJVya5g {color: rgba(0, 0, 0, 0.75);font: ;}

类的加载:Load
类的链接:Link
类的初始化:Initialize
将类的clazz文件读入内存,并为之创建一个java.lang.Clazz对象,此过程由类加载器完成
将类的二进制数据合并到JRE中
JVM负责对类进行初始化

类的加载与ClassLoader的理解

  • 加载:将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后生成一个代表这个类的java.lang.Class对象。
  • 链接:将Java类的二进制代码合并到JVM的运行状态之中的过程。
    • 验证:确保加载的类信息符合JVM规范,没有安全方面的问题
    • 准备:正式为类变量(static)分配内存并设置类变量默认初始值的阶段,这些内存都将在方法区中进行分配。
    • 解析:虚拟机常量池内的符号引用(常量名)替换为直接引用(地址)的过程。
  • 初始化
    • 执行类构造器()方法的过程。类构造器()方法是由编译期自动收集类中所有类变量的赋值动作和静态代码块中的语句合并产生的。(类构造器是构造类信息的,不是构造该类对象的构造器)。
    • 当初始化一个类的时候,如果发现其父类还没有进行初始化, 则需要先触发其父类的初始化。
    • 虚拟机会保证一个类的() 方法在多线程环境中被正确加锁和同步。
public class Test05 {public static void main(String[] args) {A a = new A();//System.out.println(a.m);System.out.println(A.m);/*具体过程:1.加载到内存,产生一个对应的class对象2.链接,链接结束后会产生m = 0(默认初始化)3.初始化:<clinit>(){System.out.println("A静态代码块初始化_1");m = 300;m = 100;}最后输出 100*/}
}class A{static {System.out.println("A静态代码块初始化_1");m = 300;}static int m =100;public A(){System.out.println("A类的无参构造初始化_2");}
}

输出:

A静态代码块初始化_1
A类的无参构造初始化_2
99
  • 此处插播一个知识点:

    • 看视频的时候看到打印的是A.m还懵了一下,为啥不是调用对象的属性?
    • 动手测试了下发现会有个警告:
    Static member accessed via instance reference.
    Shows references to static methods and fields via class instance rather than a class itself.
    

    ​ 翻译过来就是说:

    通过引用实例来访问静态成员
    通过类的实例来显示静态方法和变量的引用,而不是通过类本身
    
    • 针对静态变量,可能考虑到 实例化后的对象存在被回收的可能,因此推荐用类本身来访问静态变量(感觉很有道理:来源博客)
  • 插播知识点2:

    • 针对Class A中的m,倘若将static 语句 和 定义int 的语句互换,如下:

      class A{static int m =100;static {System.out.println("A静态代码块初始化_1");m = 300;}public A(){System.out.println("A类的无参构造初始化_2");}
      }
      
    • 此时打印出来的A.m就是300.

    • 介是为嘛呢?

      • 在初始化中:

      执行类构造器()方法的过程。类构造器()方法是由编译期自动收集类中所有类变量的赋值动作和静态代码块中的语句合并产生的。(类构造器是构造类信息的,不是构造该类对象的构造器)。

      众所周知,类变量也就是静态变量,即静态定义赋值的m会与静态代码块中合并,也就是说 此时m的值是与静态代码块的顺序有关的,这也解释了为啥调换顺序后m的值会变成300了

分析类初始化

什么时候会发生类初始化?

  • 类的主动引用(一定会发生类的初始化)

    • 当虚拟机启动,先初始化main方法所在的类
    • new一个类的对象
    • 调用类的静态成员(除了final常量)和静态方法
    • 使用java.lang.reflect包的方法对类进行反射调用
    • 当初始化一个类,如果其父类没有被初始化,则先会初始化它的父类
  • 类的被动引用(不会发生类的初始化)
    • 当访问一个静态域时,只有真正声明这个域的类才会被初始化。如:当通过子类引用父类的静态变量,不会导致子类初始化
    • 通过数组定义类引用,不会触发此类的初始化
    • 引用常量不会触发此类的初始化(常量在链接阶段就存入调用类的常量池中了)
//测试类什么时候会初始化
public class Test06 {static {System.out.println("Main类被加载...");}@Testpublic void test主动引用() throws ClassNotFoundException {//主动引用://1.实例化://Son son = new Son();//2.反射:Class.forName("com.kuangstudy.AnnotationAndReflect.Son");/* 结果* Main类被加载...* 父类被加载...* 子类被加载...*/}@Testpublic void test被动引用(){//不会产生类引用的方法:此时子类未被初始化// System.out.println(Son.f);/*Main类被加载...父类被加载...2*///通过数组:只有类被加载//Son[] sons = new Son[5];/*Main类被加载...> 因为:当虚拟机启动时,先初始化main方法所在的类* *///引用常量System.out.println(Son.M);/*Main类被加载...1*/ }
}class Father{static int f =2;static {System.out.println("父类被加载...");}}class Son extends Father{static {System.out.println("子类被加载...");s = 300;}static int s =100;static final int M = 1;
}

类加载器

类加载器的作用

  1. 将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后在堆中生成一个代表这个类的java.lang.Class对象,作为方法区中类数据的访问
    入口。

  2. 类缓存:标准的JavaSE类加载器可以按要求查找类,但一旦某个类被加载到类加载器中,它将维持加载(缓存)一段时间。不过JVM垃圾回收机制可以回收这些Class对象

    #mermaid-svg-QpsBhD4L5XggDr4Y .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .label text{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .node rect,#mermaid-svg-QpsBhD4L5XggDr4Y .node circle,#mermaid-svg-QpsBhD4L5XggDr4Y .node ellipse,#mermaid-svg-QpsBhD4L5XggDr4Y .node polygon,#mermaid-svg-QpsBhD4L5XggDr4Y .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-QpsBhD4L5XggDr4Y .node .label{text-align:center;fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .node.clickable{cursor:pointer}#mermaid-svg-QpsBhD4L5XggDr4Y .arrowheadPath{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-QpsBhD4L5XggDr4Y .flowchart-link{stroke:#333;fill:none}#mermaid-svg-QpsBhD4L5XggDr4Y .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-QpsBhD4L5XggDr4Y .edgeLabel rect{opacity:0.9}#mermaid-svg-QpsBhD4L5XggDr4Y .edgeLabel span{color:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-QpsBhD4L5XggDr4Y .cluster text{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-QpsBhD4L5XggDr4Y .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-QpsBhD4L5XggDr4Y text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-QpsBhD4L5XggDr4Y .actor-line{stroke:grey}#mermaid-svg-QpsBhD4L5XggDr4Y .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-QpsBhD4L5XggDr4Y #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .sequenceNumber{fill:#fff}#mermaid-svg-QpsBhD4L5XggDr4Y #sequencenumber{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y #crosshead path{fill:#333;stroke:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .messageText{fill:#333;stroke:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-QpsBhD4L5XggDr4Y .labelText,#mermaid-svg-QpsBhD4L5XggDr4Y .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-QpsBhD4L5XggDr4Y .loopText,#mermaid-svg-QpsBhD4L5XggDr4Y .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-QpsBhD4L5XggDr4Y .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-QpsBhD4L5XggDr4Y .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-QpsBhD4L5XggDr4Y .noteText,#mermaid-svg-QpsBhD4L5XggDr4Y .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-QpsBhD4L5XggDr4Y .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-QpsBhD4L5XggDr4Y .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-QpsBhD4L5XggDr4Y .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-QpsBhD4L5XggDr4Y .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y .section{stroke:none;opacity:0.2}#mermaid-svg-QpsBhD4L5XggDr4Y .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-QpsBhD4L5XggDr4Y .section2{fill:#fff400}#mermaid-svg-QpsBhD4L5XggDr4Y .section1,#mermaid-svg-QpsBhD4L5XggDr4Y .section3{fill:#fff;opacity:0.2}#mermaid-svg-QpsBhD4L5XggDr4Y .sectionTitle0{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .sectionTitle1{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .sectionTitle2{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .sectionTitle3{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-QpsBhD4L5XggDr4Y .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y .grid path{stroke-width:0}#mermaid-svg-QpsBhD4L5XggDr4Y .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-QpsBhD4L5XggDr4Y .task{stroke-width:2}#mermaid-svg-QpsBhD4L5XggDr4Y .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y .taskText:not([font-size]){font-size:11px}#mermaid-svg-QpsBhD4L5XggDr4Y .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-QpsBhD4L5XggDr4Y .task.clickable{cursor:pointer}#mermaid-svg-QpsBhD4L5XggDr4Y .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-QpsBhD4L5XggDr4Y .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-QpsBhD4L5XggDr4Y .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-QpsBhD4L5XggDr4Y .taskText0,#mermaid-svg-QpsBhD4L5XggDr4Y .taskText1,#mermaid-svg-QpsBhD4L5XggDr4Y .taskText2,#mermaid-svg-QpsBhD4L5XggDr4Y .taskText3{fill:#fff}#mermaid-svg-QpsBhD4L5XggDr4Y .task0,#mermaid-svg-QpsBhD4L5XggDr4Y .task1,#mermaid-svg-QpsBhD4L5XggDr4Y .task2,#mermaid-svg-QpsBhD4L5XggDr4Y .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-QpsBhD4L5XggDr4Y .taskTextOutside0,#mermaid-svg-QpsBhD4L5XggDr4Y .taskTextOutside2{fill:#000}#mermaid-svg-QpsBhD4L5XggDr4Y .taskTextOutside1,#mermaid-svg-QpsBhD4L5XggDr4Y .taskTextOutside3{fill:#000}#mermaid-svg-QpsBhD4L5XggDr4Y .active0,#mermaid-svg-QpsBhD4L5XggDr4Y .active1,#mermaid-svg-QpsBhD4L5XggDr4Y .active2,#mermaid-svg-QpsBhD4L5XggDr4Y .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-QpsBhD4L5XggDr4Y .activeText0,#mermaid-svg-QpsBhD4L5XggDr4Y .activeText1,#mermaid-svg-QpsBhD4L5XggDr4Y .activeText2,#mermaid-svg-QpsBhD4L5XggDr4Y .activeText3{fill:#000 !important}#mermaid-svg-QpsBhD4L5XggDr4Y .done0,#mermaid-svg-QpsBhD4L5XggDr4Y .done1,#mermaid-svg-QpsBhD4L5XggDr4Y .done2,#mermaid-svg-QpsBhD4L5XggDr4Y .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-QpsBhD4L5XggDr4Y .doneText0,#mermaid-svg-QpsBhD4L5XggDr4Y .doneText1,#mermaid-svg-QpsBhD4L5XggDr4Y .doneText2,#mermaid-svg-QpsBhD4L5XggDr4Y .doneText3{fill:#000 !important}#mermaid-svg-QpsBhD4L5XggDr4Y .crit0,#mermaid-svg-QpsBhD4L5XggDr4Y .crit1,#mermaid-svg-QpsBhD4L5XggDr4Y .crit2,#mermaid-svg-QpsBhD4L5XggDr4Y .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-QpsBhD4L5XggDr4Y .activeCrit0,#mermaid-svg-QpsBhD4L5XggDr4Y .activeCrit1,#mermaid-svg-QpsBhD4L5XggDr4Y .activeCrit2,#mermaid-svg-QpsBhD4L5XggDr4Y .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-QpsBhD4L5XggDr4Y .doneCrit0,#mermaid-svg-QpsBhD4L5XggDr4Y .doneCrit1,#mermaid-svg-QpsBhD4L5XggDr4Y .doneCrit2,#mermaid-svg-QpsBhD4L5XggDr4Y .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-QpsBhD4L5XggDr4Y .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-QpsBhD4L5XggDr4Y .milestoneText{font-style:italic}#mermaid-svg-QpsBhD4L5XggDr4Y .doneCritText0,#mermaid-svg-QpsBhD4L5XggDr4Y .doneCritText1,#mermaid-svg-QpsBhD4L5XggDr4Y .doneCritText2,#mermaid-svg-QpsBhD4L5XggDr4Y .doneCritText3{fill:#000 !important}#mermaid-svg-QpsBhD4L5XggDr4Y .activeCritText0,#mermaid-svg-QpsBhD4L5XggDr4Y .activeCritText1,#mermaid-svg-QpsBhD4L5XggDr4Y .activeCritText2,#mermaid-svg-QpsBhD4L5XggDr4Y .activeCritText3{fill:#000 !important}#mermaid-svg-QpsBhD4L5XggDr4Y .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-QpsBhD4L5XggDr4Y g.classGroup text .title{font-weight:bolder}#mermaid-svg-QpsBhD4L5XggDr4Y g.clickable{cursor:pointer}#mermaid-svg-QpsBhD4L5XggDr4Y g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-QpsBhD4L5XggDr4Y g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-QpsBhD4L5XggDr4Y .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-QpsBhD4L5XggDr4Y .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-QpsBhD4L5XggDr4Y .dashed-line{stroke-dasharray:3}#mermaid-svg-QpsBhD4L5XggDr4Y #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y .commit-id,#mermaid-svg-QpsBhD4L5XggDr4Y .commit-msg,#mermaid-svg-QpsBhD4L5XggDr4Y .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-QpsBhD4L5XggDr4Y g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-QpsBhD4L5XggDr4Y g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-QpsBhD4L5XggDr4Y g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-QpsBhD4L5XggDr4Y .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-QpsBhD4L5XggDr4Y .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-QpsBhD4L5XggDr4Y .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-QpsBhD4L5XggDr4Y .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-QpsBhD4L5XggDr4Y .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-QpsBhD4L5XggDr4Y .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-QpsBhD4L5XggDr4Y .edgeLabel text{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QpsBhD4L5XggDr4Y .node circle.state-start{fill:black;stroke:black}#mermaid-svg-QpsBhD4L5XggDr4Y .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-QpsBhD4L5XggDr4Y #statediagram-barbEnd{fill:#9370db}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-state .divider{stroke:#9370db}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-QpsBhD4L5XggDr4Y .note-edge{stroke-dasharray:5}#mermaid-svg-QpsBhD4L5XggDr4Y .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-QpsBhD4L5XggDr4Y .error-icon{fill:#522}#mermaid-svg-QpsBhD4L5XggDr4Y .error-text{fill:#522;stroke:#522}#mermaid-svg-QpsBhD4L5XggDr4Y .edge-thickness-normal{stroke-width:2px}#mermaid-svg-QpsBhD4L5XggDr4Y .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-QpsBhD4L5XggDr4Y .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-QpsBhD4L5XggDr4Y .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-QpsBhD4L5XggDr4Y .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-QpsBhD4L5XggDr4Y .marker{fill:#333}#mermaid-svg-QpsBhD4L5XggDr4Y .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-QpsBhD4L5XggDr4Y {color: rgba(0, 0, 0, 0.75);font: ;}

    源程序:java文件
    Java编译器
    字节码文件clazz文件
    类加载器
    字节码校验器
    解释器
    操作系统平台

  3. 类加载器作用是用来把类(class)装载进内存的。JVM规范定义了如下类型的类的加载器

    1. 引导类加载器:用C++编写的,是JVM自带的类加载器,负责Java平台核心库(rt.jar)用来装载核心类库。该加载器无法直接获取

    2. 扩展类加载器:负责jre/lib/ext目录下的jar包或-D java.ext.dirs指定目录下的jar包装入工作库

    3. 系统类加载器:负责java-classpath或-D java.class.path所指的目录下的类与jar包装入工作,是最常用的加载器自定义类加载器

      #mermaid-svg-eD03B3gGXcFAorKF .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-eD03B3gGXcFAorKF .label text{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .node rect,#mermaid-svg-eD03B3gGXcFAorKF .node circle,#mermaid-svg-eD03B3gGXcFAorKF .node ellipse,#mermaid-svg-eD03B3gGXcFAorKF .node polygon,#mermaid-svg-eD03B3gGXcFAorKF .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-eD03B3gGXcFAorKF .node .label{text-align:center;fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .node.clickable{cursor:pointer}#mermaid-svg-eD03B3gGXcFAorKF .arrowheadPath{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-eD03B3gGXcFAorKF .flowchart-link{stroke:#333;fill:none}#mermaid-svg-eD03B3gGXcFAorKF .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-eD03B3gGXcFAorKF .edgeLabel rect{opacity:0.9}#mermaid-svg-eD03B3gGXcFAorKF .edgeLabel span{color:#333}#mermaid-svg-eD03B3gGXcFAorKF .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-eD03B3gGXcFAorKF .cluster text{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-eD03B3gGXcFAorKF .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-eD03B3gGXcFAorKF text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-eD03B3gGXcFAorKF .actor-line{stroke:grey}#mermaid-svg-eD03B3gGXcFAorKF .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-eD03B3gGXcFAorKF .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-eD03B3gGXcFAorKF #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-eD03B3gGXcFAorKF .sequenceNumber{fill:#fff}#mermaid-svg-eD03B3gGXcFAorKF #sequencenumber{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF #crosshead path{fill:#333;stroke:#333}#mermaid-svg-eD03B3gGXcFAorKF .messageText{fill:#333;stroke:#333}#mermaid-svg-eD03B3gGXcFAorKF .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-eD03B3gGXcFAorKF .labelText,#mermaid-svg-eD03B3gGXcFAorKF .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-eD03B3gGXcFAorKF .loopText,#mermaid-svg-eD03B3gGXcFAorKF .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-eD03B3gGXcFAorKF .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-eD03B3gGXcFAorKF .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-eD03B3gGXcFAorKF .noteText,#mermaid-svg-eD03B3gGXcFAorKF .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-eD03B3gGXcFAorKF .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-eD03B3gGXcFAorKF .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-eD03B3gGXcFAorKF .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-eD03B3gGXcFAorKF .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF .section{stroke:none;opacity:0.2}#mermaid-svg-eD03B3gGXcFAorKF .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-eD03B3gGXcFAorKF .section2{fill:#fff400}#mermaid-svg-eD03B3gGXcFAorKF .section1,#mermaid-svg-eD03B3gGXcFAorKF .section3{fill:#fff;opacity:0.2}#mermaid-svg-eD03B3gGXcFAorKF .sectionTitle0{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .sectionTitle1{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .sectionTitle2{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .sectionTitle3{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-eD03B3gGXcFAorKF .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF .grid path{stroke-width:0}#mermaid-svg-eD03B3gGXcFAorKF .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-eD03B3gGXcFAorKF .task{stroke-width:2}#mermaid-svg-eD03B3gGXcFAorKF .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF .taskText:not([font-size]){font-size:11px}#mermaid-svg-eD03B3gGXcFAorKF .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-eD03B3gGXcFAorKF .task.clickable{cursor:pointer}#mermaid-svg-eD03B3gGXcFAorKF .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-eD03B3gGXcFAorKF .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-eD03B3gGXcFAorKF .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-eD03B3gGXcFAorKF .taskText0,#mermaid-svg-eD03B3gGXcFAorKF .taskText1,#mermaid-svg-eD03B3gGXcFAorKF .taskText2,#mermaid-svg-eD03B3gGXcFAorKF .taskText3{fill:#fff}#mermaid-svg-eD03B3gGXcFAorKF .task0,#mermaid-svg-eD03B3gGXcFAorKF .task1,#mermaid-svg-eD03B3gGXcFAorKF .task2,#mermaid-svg-eD03B3gGXcFAorKF .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-eD03B3gGXcFAorKF .taskTextOutside0,#mermaid-svg-eD03B3gGXcFAorKF .taskTextOutside2{fill:#000}#mermaid-svg-eD03B3gGXcFAorKF .taskTextOutside1,#mermaid-svg-eD03B3gGXcFAorKF .taskTextOutside3{fill:#000}#mermaid-svg-eD03B3gGXcFAorKF .active0,#mermaid-svg-eD03B3gGXcFAorKF .active1,#mermaid-svg-eD03B3gGXcFAorKF .active2,#mermaid-svg-eD03B3gGXcFAorKF .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-eD03B3gGXcFAorKF .activeText0,#mermaid-svg-eD03B3gGXcFAorKF .activeText1,#mermaid-svg-eD03B3gGXcFAorKF .activeText2,#mermaid-svg-eD03B3gGXcFAorKF .activeText3{fill:#000 !important}#mermaid-svg-eD03B3gGXcFAorKF .done0,#mermaid-svg-eD03B3gGXcFAorKF .done1,#mermaid-svg-eD03B3gGXcFAorKF .done2,#mermaid-svg-eD03B3gGXcFAorKF .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-eD03B3gGXcFAorKF .doneText0,#mermaid-svg-eD03B3gGXcFAorKF .doneText1,#mermaid-svg-eD03B3gGXcFAorKF .doneText2,#mermaid-svg-eD03B3gGXcFAorKF .doneText3{fill:#000 !important}#mermaid-svg-eD03B3gGXcFAorKF .crit0,#mermaid-svg-eD03B3gGXcFAorKF .crit1,#mermaid-svg-eD03B3gGXcFAorKF .crit2,#mermaid-svg-eD03B3gGXcFAorKF .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-eD03B3gGXcFAorKF .activeCrit0,#mermaid-svg-eD03B3gGXcFAorKF .activeCrit1,#mermaid-svg-eD03B3gGXcFAorKF .activeCrit2,#mermaid-svg-eD03B3gGXcFAorKF .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-eD03B3gGXcFAorKF .doneCrit0,#mermaid-svg-eD03B3gGXcFAorKF .doneCrit1,#mermaid-svg-eD03B3gGXcFAorKF .doneCrit2,#mermaid-svg-eD03B3gGXcFAorKF .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-eD03B3gGXcFAorKF .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-eD03B3gGXcFAorKF .milestoneText{font-style:italic}#mermaid-svg-eD03B3gGXcFAorKF .doneCritText0,#mermaid-svg-eD03B3gGXcFAorKF .doneCritText1,#mermaid-svg-eD03B3gGXcFAorKF .doneCritText2,#mermaid-svg-eD03B3gGXcFAorKF .doneCritText3{fill:#000 !important}#mermaid-svg-eD03B3gGXcFAorKF .activeCritText0,#mermaid-svg-eD03B3gGXcFAorKF .activeCritText1,#mermaid-svg-eD03B3gGXcFAorKF .activeCritText2,#mermaid-svg-eD03B3gGXcFAorKF .activeCritText3{fill:#000 !important}#mermaid-svg-eD03B3gGXcFAorKF .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-eD03B3gGXcFAorKF g.classGroup text .title{font-weight:bolder}#mermaid-svg-eD03B3gGXcFAorKF g.clickable{cursor:pointer}#mermaid-svg-eD03B3gGXcFAorKF g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-eD03B3gGXcFAorKF g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-eD03B3gGXcFAorKF .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-eD03B3gGXcFAorKF .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-eD03B3gGXcFAorKF .dashed-line{stroke-dasharray:3}#mermaid-svg-eD03B3gGXcFAorKF #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF .commit-id,#mermaid-svg-eD03B3gGXcFAorKF .commit-msg,#mermaid-svg-eD03B3gGXcFAorKF .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-eD03B3gGXcFAorKF g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-eD03B3gGXcFAorKF g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-eD03B3gGXcFAorKF g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-eD03B3gGXcFAorKF .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-eD03B3gGXcFAorKF .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-eD03B3gGXcFAorKF .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-eD03B3gGXcFAorKF .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-eD03B3gGXcFAorKF .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-eD03B3gGXcFAorKF .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-eD03B3gGXcFAorKF .edgeLabel text{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-eD03B3gGXcFAorKF .node circle.state-start{fill:black;stroke:black}#mermaid-svg-eD03B3gGXcFAorKF .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-eD03B3gGXcFAorKF #statediagram-barbEnd{fill:#9370db}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-state .divider{stroke:#9370db}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-eD03B3gGXcFAorKF .note-edge{stroke-dasharray:5}#mermaid-svg-eD03B3gGXcFAorKF .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-eD03B3gGXcFAorKF .error-icon{fill:#522}#mermaid-svg-eD03B3gGXcFAorKF .error-text{fill:#522;stroke:#522}#mermaid-svg-eD03B3gGXcFAorKF .edge-thickness-normal{stroke-width:2px}#mermaid-svg-eD03B3gGXcFAorKF .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-eD03B3gGXcFAorKF .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-eD03B3gGXcFAorKF .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-eD03B3gGXcFAorKF .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-eD03B3gGXcFAorKF .marker{fill:#333}#mermaid-svg-eD03B3gGXcFAorKF .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-eD03B3gGXcFAorKF {color: rgba(0, 0, 0, 0.75);font: ;}

      Bootstap_Classloader
      Extension_Classloader
      System_Classloader
      自定义类加载器

      自底向上检查类是否已装载(自定义 -->Bootstap Classloader)

      自顶向下尝试加载类(Bootstap Classloader --> 自定义)

      上述可以说是简单版的双亲委派机制的理解:你不配加载我的类

    public class Test07 {public static void main(String[] args) {//获取系统的类加载器:ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();System.out.println(systemClassLoader);//sun.misc.Launcher$AppClassLoader@18b4aac2//获取系统类加载器的父类加载器 -->扩展类加载器ClassLoader parent = systemClassLoader.getParent();System.out.println(parent);//sun.misc.Launcher$ExtClassLoader@12a3a380///获取扩展类加载器的父类加载器 --> 根加载器(C/C++)ClassLoader parent1 = parent.getParent();System.out.println(parent1);//null}@Testpublic void testClassloader() throws ClassNotFoundException {//测试当前类是哪个加载器:ClassLoader classLoader = Class.forName("com.kuangstudy.AnnotationAndReflect.Test07").getClassLoader();System.out.println(classLoader);//sun.misc.Launcher$AppClassLoader@18b4aac2//说明是系统的类加载器加载的//测试JDK内置的类是谁加载的classLoader = Class.forName("java.lang.Object").getClassLoader();System.out.println(classLoader); //null//lang 包在 rt.jar中,是根加载器加载的}@Testpublic void test加载路径(){//如何获取系统类加载器可以加载的路径:  System.out.println(System.getProperty("java.class.path"));/*...C:\Java\jdk1.80_201\jre\lib\plugin.jar;C:\Java\jdk1.80_201\jre\lib\resources.jar;C:\Java\jdk1.80_201\jre\lib\rt.jar;...D:\IdeaProjects\itstack-demo-design-master\itstack-demo-design-1-00\target\classes;...*/}
    }
    

获取类运行时结构

通过反射获取运行时类的完整结构
Field 、 Method 、 Constructor 、 Superclass 、 Interface 、 Annotation

  • 实现的全部接口

  • 所继承的父类

  • 全部的构造器

  • 全部的方法

  • 全部的Field

  • 注解

//获取类的信息
public class Test08 {public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {Class c1 = Class.forName("com.kuangstudy.AnnotationAndReflect.User");//获取类的名字://获取包名+类名 :private native String getName0();System.out.println(c1.getName()); //com.kuangstudy.AnnotationAndReflect.User//获取类名:System.out.println(c1.getSimpleName());//User//获取类的属性Field[] fields = c1.getFields();//只能找到public属性:源码强调:Member.PUBLIC,for (Field field : fields) {System.out.println(field);}fields = c1.getDeclaredFields();for (Field field : fields) {System.out.println(field);//能找到全部属性:Member.DECLARED}//获取指定属性的值:Field name = c1.getDeclaredField("name");System.out.println(name);//获取类的方法Method[] methods = c1.getMethods();//可以获得本类以及父类的全部public方法Method[] declaredMethods = c1.getDeclaredMethods();//可以打印本类的所有方法//指定方法//需要参数,避免重载的情况Method getName = c1.getMethod("getName", null);Method setName = c1.getMethod("setName", String.class);System.out.println(getName);//public java.lang.String com.kuangstudy.AnnotationAndReflect.User.getName()System.out.println(setName);//public void com.kuangstudy.AnnotationAndReflect.User.setName(java.lang.String)}
}

动态创建对象执行方法

小结

  • 在实际的操作中,取得类的信息的操作代码,并不会经常开发
  • 一定要熟悉java.lang.reflect包的作用,反射机制。
  • 如何取得属性、方法、构造器的名称,修饰符等。

有了Class对象,能做什么?

  • 创建类的对象:调用Class对象的newlnstance()方法

    1. 类必须有一个无参数的构造器。
    2. 类的构造器的访问权限需要足够
  • **思考?**难道没有无参的构造器就不能创建对象了吗?只要在操作的时候明确的调用类中的构造器,并将参数传递进去之后,才可以实例化操作。
    • 步骤如下:

      1. 通过Class类的getDeclaredConstructor(Class…parameterTypes)取得本类的指定形参类型的构造器
      2. 向构造器的形参中传递一个对象数组进去,里面包含了构造器中所需的各个参数。
      3. 通过Constructor实例化对象

调用指定的方法

通过反射,调用类中的方法,通过Method类完成。

  1. 通过Class类的getMethod(String name,Class…parameterTypes)方法取得一个Method对象,并设置此方法操作时所需要的参数类型。
  2. 之后使用Object invoke(Object obj,Object[]args)进行调用,并向方法中传
    递要设置的obj对象的参数信息。
  3. Object invoke(Object obj, Object ... args)
    1. Object对应原方法的返回值,若原方法无返回值,此时返回null
    2. 若原方法若为静态方法,此时形参Object obj可为null
    3. 若原方法形参列表为空,则Object[]args为null
    4. 若原方法声明为private,则需要在调用此invoke()方法前,显式调用方法对象的setAccessible(true)方法,将可访问private的方法。
    5. Method和Field、Constructor对象都有setAccessible()方法。
    6. setAccessible作用是启动和禁用访问安全检查的开关。
    7. 参数值为true则指示反射的对象在使用时应该取消Java语言访问检查。
      1. 提高反射的效率。如果代码中必须用反射,而该句代码需要频繁的被调用,那么请设置为true。
      2. 使得原本无法访问的私有成员也可以访问
    8. 参数值为false则指示反射的对象应该实施Java语言访问检查
//通过反射,动态创建对象
public class Test09 {@Testpublic void test构造一个对象() throws ClassNotFoundException, IllegalAccessException, InstantiationException {//获取class对象Class cl1 = Class.forName("com.kuangstudy.AnnotationAndReflect.User");//构造一个对象User user = (User) cl1.newInstance();//本质上调用了无参构造器System.out.println(user);//手动将User的无参构造器删除后,/*输出结果:Exception in thread "main" java.lang.InstantiationException:com.kuangstudy.AnnotationAndReflect.User*/}@Testpublic void test通过构造器创建对象() throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {//获取class对象Class cl1 = Class.forName("com.kuangstudy.AnnotationAndReflect.User");//通过构造函数创建对象Constructor constructor = cl1.getDeclaredConstructor(String.class, int.class, int.class);User wayne = (User) constructor.newInstance("Wayne", 001, 23);System.out.println(wayne);//User{name='Wayne', id=1, age=23}}@Testpublic void test通过反射调用普通方法() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {//获取class对象Class cl1 = Class.forName("com.kuangstudy.AnnotationAndReflect.User");User user3 = (User) cl1.newInstance();//通过反射获取一个方法//invoke:激活  (对象,"方法的值")Method setName = cl1.getDeclaredMethod("setName", String.class);setName.invoke(user3, "Wayne");System.out.println(user3.getName());}@Testpublic void test通过反射操作属性() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {//获取class对象Class cl1 = Class.forName("com.kuangstudy.AnnotationAndReflect.User");User user4 = (User) cl1.newInstance();Field name = cl1.getDeclaredField("name");//直接操作属性会报权限异常:java.lang.IllegalAccessException: Class com.kuangstudy.AnnotationAndReflect.Test09 can not access a member of class com.kuangstudy//通过反射去除安全检测:name.setAccessible(true);//后来加上去的name.set(user4,"Wayne");//不然这里也会报错System.out.println(user4.getName());//Wayne//总结://不能直接操作私有属性,我们需要打开访问权限,修改属性或者方法的.setAccessible(true)方法即可.}
}

性能对比分析

setAccessible()方法

  • Method和Field、Constructor对象都有setAccessible()方法。
  • setAccessible作用是启动和禁用访问安全检查的开关。
  • 参数值为true则指示反射的对象在使用时应该取消Java语言访问检查。
    • 提高反射的效率。如果代码中必须用反射,而该句代码需要频繁的被调用,那么请设置为true。
    • 使得原本无法访问的私有成员也可以访问
  • 参数值为false则指示反射的对象应该实施Java语言访问检查
//分析性能问题
public class Test10 {//普通方法调用public static void test01(){User user = new User();long startTime = System.currentTimeMillis();for (int i = 0; i < 1000000000; i++) {user.getName();}long endTime = System.currentTimeMillis();System.out.println("普通方法执行10亿次:              "+(endTime - startTime)+"ms");//普通方法执行10亿次:2297ms}//反射方式调用public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {User user = new User();Class cl1 = user.getClass();Method getName = cl1.getDeclaredMethod("getName", null);long startTime = System.currentTimeMillis();for (int i = 0; i < 1000000000; i++) {getName.invoke(user,null);}long endTime = System.currentTimeMillis();System.out.println("反射方式调用执行10亿次:         "+(endTime - startTime)+"ms");}//反射方式调用,关闭安全检测(打开访问权限)public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {User user = new User();Class cl1 = user.getClass();Method getName = cl1.getDeclaredMethod("getName", null);getName.setAccessible(true);long startTime = System.currentTimeMillis();for (int i = 0; i < 1000000000; i++) {getName.invoke(user,null);}long endTime = System.currentTimeMillis();System.out.println("反射方式(关闭安全检测)调用10亿次:"+(endTime - startTime)+"ms");}public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {test01();test02();test03();}
}

输出结果:

普通方法执行10亿次:              8ms
反射方式调用执行10亿次:         3198ms
反射方式(关闭安全检测)调用10亿次:1504ms

可见执行性能:

普通方法 > (关闭检测)反射 > 普通反射调用

获取泛型信息

反射操作泛型

  • Java采用泛型擦除的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据的安全性和免去强制类型转换问题,但是,一旦编译完成,所有和泛型有关的类型全部擦除
  • 为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType
    TypeVariable和WildcardType几种类型来代表不能被归一到Class类中的类型但是又和原
    始类型齐名的类型.
  • ParameterizedType:表示一种参数化类型(其实就是泛型),比如Collection
  • GenericArrayType:表示一种元素类型是参数化类型或者类型变量的数组类型
  • TypeVariable:是各种类型变量的公共父接口
  • WildcardType:代表一种通配符类型表达式
//通过反射获取泛型
public class Test11 {public void test01(Map<String,User> map, List<User> list){System.out.println("test01");}public Map<String,User> test02(){System.out.println("test02");return null;}public int test03(int a, boolean b, char c ,short s,long l ,byte b2, double d,float f,String s1){return 1+1;}/**** method中的getGenericParameterTypes()方法* public Type[] getGenericParameterTypes()* 返回一个Type对象的数组, Type以声明顺序表示由该对象表示的可执行文件的形式参数类型。 如果底层可执行文件没有参数,则返回长度为0的数组。* 如果形式参数类型是参数化类型,那么Type返回的Type对象必须准确地反映源代码中使用的实际类型参数。** 如果形式参数类型是类型变量或参数化类型,则会创建它。 否则解决。*/private void getMyParamterizedType(String methodName, Type[] genericParameterTypes) {//获取形参,如果类型是 泛型类型则需打印出泛型的参数。int i=1;for (Type genericParameterType : genericParameterTypes) {System.out.println(methodName+"参数类型"+(i++)+":"+genericParameterType);if (genericParameterType instanceof ParameterizedType){Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();System.out.println(methodName+"泛型实际参数:"+Arrays.toString(actualTypeArguments));}}}@Testpublic void ttest01() throws NoSuchMethodException {//获取Test11中的test01方法Method test01 = Test11.class.getMethod("test01", Map.class, List.class);//获取test1中的形参Type[] genericParameterTypes = test01.getGenericParameterTypes();getMyParamterizedType(test01.getName(), genericParameterTypes);/*test01参数类型1:java.util.Map<java.lang.String, com.kuangstudy.AnnotationAndReflect.User>test01泛型实际参数:[class java.lang.String, class com.kuangstudy.AnnotationAndReflect.User]test01参数类型2:java.util.List<com.kuangstudy.AnnotationAndReflect.User>test01泛型实际参数:[class com.kuangstudy.AnnotationAndReflect.User]*/}@Testpublic void ttest02() throws NoSuchMethodException {//获取Test11中的test02方法Method test02 = Test11.class.getMethod("test02");//获取test2中的形参Type[] genericParameterTypes = test02.getGenericParameterTypes();getMyParamterizedType(test02.getName(),genericParameterTypes);//因为test02是空参方法,所以打印结果为空}@Testpublic void ttest03() throws NoSuchMethodException {//获取Test11中的test03方法Method test03 = Test11.class.getMethod("test03", int.class, boolean.class, char.class, short.class, long.class, byte.class, double.class, float.class, String.class);//获取test3中形参Type[] genericParameterTypes = test03.getGenericParameterTypes();getMyParamterizedType(test03.getName(),genericParameterTypes);/*test03参数类型1:inttest03参数类型2:booleantest03参数类型3:chartest03参数类型4:shorttest03参数类型5:longtest03参数类型6:bytetest03参数类型7:doubletest03参数类型8:floattest03参数类型9:class java.lang.String*/}
}

获取注解信息

通过反射操作注解

  • getAnnotations
  • getAnnotation

利用注解和反射完成类和表结构的反射关系

ORM原理

//练习反射操作注解
public class Test12 {public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {Class cl1 = Class.forName("com.kuangstudy.AnnotationAndReflect.Student2");//通过反射获取注解:获取最外层的注解Annotation[] annotations = cl1.getAnnotations();for (Annotation annotation : annotations) {System.out.println(annotation);//@com.kuangstudy.AnnotationAndReflect.TableWayne(value=db_student)}//获取类指定注解的value:TableWayne tableWayne = (TableWayne)cl1.getAnnotation(TableWayne.class);String[] split = tableWayne.toString().split("\\.");System.out.println(split[3].substring(0,split[3].indexOf('('))+"  value值为:"+tableWayne.value());//获取属性指定的注解Field field = cl1.getDeclaredField("name");FieldWayne annotation = field.getDeclaredAnnotation(FieldWayne.class);String anno_name = annotation.toString();String name = anno_name.substring(anno_name.lastIndexOf('.')+1,anno_name.indexOf('('));System.out.println(name+":"+annotation.columnName());System.out.println(name+":"+annotation.type());System.out.println(name+":"+annotation.length());/*@com.kuangstudy.AnnotationAndReflect.TableWayne(value=db_student)TableWayne  value值为:db_studentFieldWayne:db_nameFieldWayne:varcharFieldWayne:10*/}}@TableWayne("db_student")
class Student2{@FieldWayne(columnName = "db_id",type = "int",length = 10)private int id;@FieldWayne(columnName = "db_age",type = "int",length = 10)private int age;@FieldWayne(columnName = "db_name",type = "varchar",length = 10)private String name;public Student2() {}public Student2(int id, int age, String name) {this.id = id;this.age = age;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Student2{" +"id=" + id +", age=" + age +", name='" + name + '\'' +'}';}
}//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface  TableWayne{String value();
}//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldWayne{String columnName();String type();int length();
}

厚积薄发打卡Day24 :狂神说Java之注解与反射<全网最全(代码+笔记)>相关推荐

  1. 厚积薄发打卡Day25 :狂神说Java之多线程详解<全网最全(代码+笔记)>

    概述 视频来源:[狂神说Java]多线程详解 强烈推荐,

  2. Java基于注解和反射导入导出Excel

    代码地址如下: http://www.demodashi.com/demo/11995.html 1. 构建项目 使用Spring Boot快速构建一个Web工程,并导入与操作Excel相关的POI包 ...

  3. Java基础-注解和反射

    Java基础-注解和反射 前言 对于注解,我主要还是在自定义APT还有运行时反射获取类来让自己能够构建出复用性更高的代码逻辑. 知识点1-注解: 注解的应用场景由元注解@Retention来进行指定, ...

  4. java基础- 注解和反射

    1. 注解(Annotation) 1. 什么是注解 Annotation是从JDK5.0开始引入的新技术. Annotation的作用 : 不是程序本身,可以对程序作出解释.(这一点和注释(comm ...

  5. (Java)注解和反射

    文章目录 注解和反射 一. 注解 1.1 元注解 1.2 内置注解 1.3 自定义注解 二. 反射 2.1 什么是反射 2.2 Class类 2.3 创建Class类的方式 2.4 所有类型的Clas ...

  6. Java:注解和反射

    (一)注解 1注解入门 Annotation是jdk1.5开始引入的新技术. Annotation的作用: (1)不是程序本身,可以对程序作出解释: (2)可以被其他程序(例如编译器)读取. Anno ...

  7. Java中注解与反射的使用方法及场景,强行解释一波!

    作者:BudingCode blog.csdn.net/m0_55221239/article/details/115025182 注解 注解定义 Java 注解(Annotation)又称 Java ...

  8. 第一篇博客,java学生管理系统(挑战全网最全)

    java学生信息管理系统,(课设必备),附有源码和简版链接 博主虽然技术不高,但是系统写的真的是没话说,留着开学java课设用了. 直接转载链接了,查看系统入口 https://blog.csdn.n ...

  9. java随机生成字母用三元运算符,【代码笔记】Java常识性基础补充(一)——赋值运算符、逻辑运算符、三元运算符、Scanner类、键盘输入、Random类、随机数...

    为什么要进行Java常识性基础补充? 之前学习Java语言,学得很多很杂,而且是很多不同的方面插入讲解的,比如在跟班上课,自学java编程例子,java语法,过了很久,因为各种原因长时间不怎么写,有时 ...

最新文章

  1. 有符号整型的数据范围为什么负数比正数多一个?
  2. 各地结婚年龄出炉,哪个地方的人最晚婚?
  3. C#学习小记14求助一道让我头疼的C#小题
  4. Rabbitmq集群高可用部署详细
  5. Excel 2010 下拉菜单的制作方法
  6. Linux系统下,MySQL以及禅道的安装/卸载
  7. python中大括号是什么_Python中模块(Module)和包(Package)到底是什么,有什么区别?...
  8. Ubuntu中出现“Could not get lock /var/lib/dpkg/lock”的解决方法
  9. mybatis if标签字符串判断
  10. vbs获取程序窗体句柄_VBS调用windows api函数(postmessage)实现后台发送按键脚本...
  11. AirBuddy技巧:如何检查Mac电脑是否支持低功耗蓝牙?
  12. 计算机课题名称怎么取,课题名称:微型计算机操作入门
  13. matlab 频率分辨率,功率谱、频率分辨率、频谱泄漏与窗函数
  14. 为什么要有升余弦滤波器和无码间串扰?
  15. Python图像(字母数字)识别
  16. POI excel插入图表
  17. 绝对爆笑,虽然我知道可能和别的人雷同,但欢声笑语不雷同不是么?
  18. 华为隐藏鸿蒙,鸿蒙系统有隐私空间吗_华为鸿蒙系统有隐私空间吗
  19. 最短路计数(dp+最短路)
  20. CodeBlocks监视窗口(Watchs)进行调试(引用类型与指针)

热门文章

  1. 孙多洋《融资智慧》光盘内容提要
  2. 《数据治理与数据安全》读书笔记(下)
  3. 18.04.02 luoguP1332 血色先锋队
  4. [随笔]_2010-04-07 23:33:37,宿舍没停电
  5. 2018-8-10-C#-判断文件编码
  6. cmake源码静态编译
  7. Windows自带磁盘管理工具——diskpart
  8. Ubuntu18.04 解决有线网络连接不显示
  9. 炉石胖枫抽到什么刀片服务器准系统整机主板,炉石传说砰砰计划胖枫奇数防战解析_炉石传说砰砰计划胖枫奇数防战卡组思路_牛游戏网...
  10. Java面试—蔚来汽车