java类什么时候初始化_java什么时候发生类的初始化

会发生类初始化的情况(5点)

1.虚拟机启动时,初始化main方法所在的类。

2.调用类的静态成员(final修饰除外),静态方法。

3.new一个类的对象

4.初始化一个类,其父类先被初始化,

下面一个例子概括前4点

public class Text {

static {

System.out.println("main类被加载!");

}

public static void main(String[] args) throws ClassNotFoundException {

Son son =new Son();

}

}

class Son extends Father{ //Son类继承Father类

static {

System.out.println("Son类被加载!");

}

}

class Father{

static {

System.out.println("Father类被加载!");

}

}

运行结果是:

main类被加载!

Father类被加载!

Son类被加载!

5.使用java.lang.reflect包的按方法对类反射调用

上例子:

public class Text03 {

static {

System.out.println("main类被加载!");

}

public static void main(String[] args) throws ClassNotFoundException {

Class.forName("text.Son");

}

}

class Son extends Father{

static {

System.out.println("Son类被加载!");

}

}

class Father{

static {

System.out.println("Father类被加载!");

}

}

运行结果,与实例化Son类一样:

main类被加载!

Father类被加载!

Son类被加载!

不会发生类初始化(3点)

1.子类引用父类变量

如下:在Father类中声明一个静态变量

static int fat=100;

接下来,我们直接用子类来引用父类中的静态变量,代码如下:

public class Text03 {

static {

System.out.println("main类被加载!");

}

public static void main(String[] args) throws ClassNotFoundException {

System.out.println(Son.fat);

}

}

class Son extends Father{

static {

System.out.println("Son类被加载!");

}

}

class Father{

static int fat=100;

static {

System.out.println("Father类被加载!");

}

}

结果显示:

main类被加载!

Father类被加载!

100

在输出语句中可以看出,子类引用父类静态变量是,没有被初识化

2.引用常量

如果我们把变量换成常量,即用final修饰,不可被重写

static final int fat=100;

其他代码与上述不变

public class Text03 {

static {

System.out.println("main类被加载!");

}

public static void main(String[] args) throws ClassNotFoundException {

System.out.println(Son.fat);

}

}

class Son extends Father{

static {

System.out.println("Son类被加载!");

}

}

class Father{

static final int fat=100;

static {

System.out.println("Father类被加载!");

}

}

结果显示:

main类被加载!

100

没有出现父类被初识化

因为:常量在链接阶段,就存入调用类的常量池中了

3.通过数组定义 类引用,也不会触发此类的初识化

public class Text03 {

static {

System.out.println("main类被加载!");

}

public static void main(String[] args) throws ClassNotFoundException {

Son [] son=new Son[5];

}

}

class Son extends Father{

static {

System.out.println("Son类被加载!");

}

}

class Father{

static {

System.out.println("Father类被加载!");

}

}

输出结果:

main类被加载!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值