Ask a question
What are the situations of active use and passive use of Java classes? ? ?
Solve the problem
Active use of Java classes will lead to the initialization of the class:
1) Create an instance of the class
2) Access a certain class Or a static variable of the interface, or assign a value to the static variable
3) Call the static method of the class
4) Reflection (such as Class.forName("com.bunny.Test"))
5) Initialize a subclass of a class
6) The class that is indicated as the startup class (JavaTest) when the Java virtual machine starts Are considered passive uses and do not cause the class to be initialized.
[code]package com.evada.de; class ChildClass extends ClassUsed{ public static int c = 0; } /** * Created by Ay on 2016/5/24. */ public class ClassUsed { public static int a = 0; public static void main(String[] args) throws Exception{ /** 创建类的实例 **/ ClassUsed classUsed = new ClassUsed(); /** 访问某个类或接口的静态变量,或者对该静态变量赋值 **/ int b = ClassUsed.a; /** 调用类的静态方法 **/ ClassUsed.test(); /** 反射 **/ Class.forName("com.bunny.Test"); /** 初始化一个类的子类 **/ ChildClass.c = 10; /** Java虚拟机启动时被表明为启动类的类 **/ //java com.hwy.MyTest } public static void test(){ } }