主动使用和被动使用说区分的关键是会不会调用()这个方法
类的主动使用
package com.atguigu.java1;
import org.junit.Test;
import java.io.*;
/**
* @author shkstart
* @create 2020-09-14 16:37
*
* 测试类的主动使用:意味着会调用类的<clinit>(),即执行了类的初始化阶段
*
* 1. 当创建一个类的实例时,比如使用new关键字,或者通过反射、克隆、反序列化。
* 2. 当调用类的静态方法时,即当使用了字节码invokestatic指令。
*/
public class ActiveUse1 {
public static void main(String[] args) {
Order order = new Order();
}
//序列化的过程:
@Test
public void test1() {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("order.dat"));
oos.writeObject(new Order());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//反序列化的过程:(验证)
@Test
public void test2() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("order.dat"));
Order order = (Order) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test3(){
Order.method();
}
}
class Order implements Serializable{
static {
System.out.println("Order类的初始化过程");
}
public static void method(){
System.out.println("Order method()....");
}
}
package com.atguigu.java1;
import org.junit.Test;
import java.util.Random;
/**
* @author shkstart
* @create 2020-09-14 16:49
*
* 3. 当使用类、接口的静态字段时(final修饰特殊考虑),比如,使用getstatic或者putstatic指令。(对应访问变量、赋值变量操作)
*
*/
public class ActiveUse2 {
@Test
public void test1(){
// System.out.println(User.num);
// System.out.println(User.num1);
System.out.println(User.num2);
}
@Test
public void test2(){
// System.out.println(CompareA.NUM1);
System.out.println(CompareA.NUM2);
}
}
class User{
static{
System.out.println("User类的初始化过程");
}
public static int num = 1;
public static final int num1 = 1;
public static final int num2 = new Random().nextInt(10);
}
interface CompareA{
public static final Thread t = new Thread(){
{
System.out.println("CompareA的初始化");
}
};
public static final int NUM1 = 1;
public static final int NUM2 = new Random().nextInt(10);
}
package com.atguigu.java1;
import org.junit.Test;
import java.util.Random;
/**
* @author shkstart
* @create 2020-09-14 17:00
* <p>
* 4. 当使用java.lang.reflect包中的方法反射类的方法时。比如:Class.forName("com.atguigu.java.Test")
* 5. 当初始化子类时,如果发现其父类还没有进行过初始化,则需要先触发其父类的初始化。
* 6. 如果一个接口定义了default方法,那么直接实现或者间接实现该接口的类的初始化,该接口要在其之前被初始化。
* 7. 当虚拟机启动时,用户需要指定一个要执行的主类(包含main()方法的那个类),虚拟机会先初始化这个主类。
* 8. 当初次调用 MethodHandle 实例时,初始化该 MethodHandle 指向的方法所在的类。
* (涉及解析REF_getStatic、REF_putStatic、REF_invokeStatic方法句柄对应的类)
* <p>
* <p>
* 针对5,补充说明:
* 当Java虚拟机初始化一个类时,要求它的所有父类都已经被初始化,但是这条规则并不适用于接口。
* >在初始化一个类时,并不会先初始化它所实现的接口
* >在初始化一个接口时,并不会先初始化它的父接口
* 因此,一个父接口并不会因为它的子接口或者实现类的初始化而初始化。只有当程序首次使用特定接口的静态字段时,
* 才会导致该接口的初始化。
*/
public class ActiveUse3 {
static{
System.out.println("ActiveUse3的初始化过程");
}
@Test
public void test1() {
try {
Class clazz = Class.forName("com.atguigu.java1.Order");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void test2() {
System.out.println(Son.num);
}
@Test
public void test3(){
System.out.println(CompareC.NUM1);
}
@Test
public void test4() {
System.out.println(Son.num);
}
public static void main(String[] args) {
System.out.println("hello");
}
}
class Father {
static {
System.out.println("Father类的初始化过程");
}
}
class Son extends Father implements CompareB{
static {
System.out.println("Son类的初始化过程");
}
public static int num = 1;
}
interface CompareB {
public static final Thread t = new Thread() {
{
System.out.println("CompareB的初始化");
}
};
public default void method1(){
System.out.println("你好!");
}
}
interface CompareC extends CompareB {
public static final Thread t = new Thread() {
{
System.out.println("CompareC的初始化");
}
};
public static final int NUM1 = new Random().nextInt();
}
被动使用
package com.atguigu.java1;
import org.junit.Test;
/**
* @author shkstart
* @create 2020-09-14 17:24
*
* 关于类的被动使用,即不会进行类的初始化操作,即不会调用<clinit>()
*
* 1. 当访问一个静态字段时,只有真正声明这个字段的类才会被初始化。
* > 当通过子类引用父类的静态变量,不会导致子类初始化
* 2. 通过数组定义类引用,不会触发此类的初始化
*
* 说明:没有初始化的类,不意味着没有加载!
*/
public class PassiveUse1 {
@Test
public void test1(){
System.out.println(Child.num);
}
@Test
public void test2(){
Parent[] parents = new Parent[10];
System.out.println(parents.getClass());
System.out.println(parents.getClass().getSuperclass());
parents[0] = new Parent();
parents[1] = new Parent();
}
}
class Parent{
static{
System.out.println("Parent的初始化过程");
}
public static int num = 1;
}
class Child extends Parent{
static{
System.out.println("Child的初始化过程");
}
}
package com.atguigu.java1;
import org.junit.Test;
import java.util.Random;
/**
* @author shkstart
* @create 2020-09-14 17:30
*
* * 3. 引用常量不会触发此类或接口的初始化。因为常量在链接阶段就已经被显式赋值了。
* * 4. 调用ClassLoader类的loadClass()方法加载一个类,并不是对类的主动使用,不会导致类的初始化。
*/
public class PassiveUse2 {
@Test
public void test1(){
// System.out.println(Person.NUM);
System.out.println(Person.NUM1);
}
@Test
public void test2(){
// System.out.println(SerialA.ID);
System.out.println(SerialA.ID1);
}
@Test
public void test3(){
try {
Class clazz = ClassLoader.getSystemClassLoader().loadClass("com.atguigu.java1.Person");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class Person{
static{
System.out.println("Person类的初始化");
}
public static final int NUM = 1;//在链接过程的准备环节就被赋值为1了。
public static final int NUM1 = new Random().nextInt(10);//此时的赋值操作需要在<clinit>()中执行
}
interface SerialA{
public static final Thread t = new Thread() {
{
System.out.println("SerialA的初始化");
}
};
int ID = 1;
int ID1 = new Random().nextInt(10);//此时的赋值操作需要在<clinit>()中执行
}