“ .class”获取类的方式-是否初始化类? [英] ".class" way of obtaining Class - does it initialize the class?

查看:161
本文介绍了“ .class”获取类的方式-是否初始化类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题与 Java 有关。有三种获取类型的类的方法(请注意,代码只是用于演示的代码片段的集合):

Question is about Java. There are three ways of obtaining classes of a type (note that code is just a collection of snippets for demonstration):

Class c1 = Class.forName("com.mypkg.MyClass"); //assumes initialize = true
// - OR - 
Class c1 = Class.forName("com.mypkg.MyClass", true/false,
                         this.getClass().getClassLoader());

Class c2 = com.mypkg.MyClass.class;
// - OR - 
import com.mypkg.MyClass;
Class c2 = MyClass.class;

MyClass mc = new MyClass();
Class c3 = mc.getClass();

我的问题是关于初始化的。对于方法1,我可以使用boolean参数来控制是否初始化类。对于方法3,由于已创建对象,因此显然已初始化了该类(对吗?)。

My question is about initialization. For method 1, I can control whether class is initialized or not using the boolean argument. For method 3, since an object is created, obviously the class is initialized (right?).

但是方法2呢? 如果尚未初始化 .class ,是否可以调用该类?
并且如何通过编程检查

But what about method 2? Does calling .class initialize the class if it has not already been initialized?
And how can one programmatically check whether a class is already initialized or not?

请参见已接受的解决方案和完整评论的评论。这只是一个摘要。

调用 .class 不会不是如果尚未初始化该类,则将其初始化。您可以通过在类定义中使用静态块来打印消息来检查类是否正在初始化。

Calling .class does not initialize the class if it has not already been initialized. You can check whether a class is getting initialized by using a static block in the class definition that prints a message.

根据 Class.forName 的javadoc,对forName( X)的调用导致名为X的类被初始化。 。另外,还有两个 Class.forName 方法,其中一个方法接受名为 initialize 的布尔参数。根据javadoc,仅当initialize参数为true且之前未初始化时,该类才会初始化。

According to javadoc of Class.forName, "A call to forName("X") causes the class named X to be initialized.". Also there are two Class.forName methods including one that accepts a boolean parameter called initialize. According to javadoc, "The class is initialized only if the initialize parameter is true and if it has not been initialized earlier."

推荐答案

如何自己进行测试,看看您的VM做什么?

How about testing it yourself, to see what your VM does?

只需将此类与所有三种方法一起使用(在独立运行中,

Just use this class with all three methods (in independent runs, as it will only be initialized once at most!)

class Example {
    static {
        System.out.println("Class was initialized!");
    }

    public static int bananas = 0;
}

有关详细信息 类别为应该进行初始化,请参见 http://docs.oracle.com/ javase / specs /#12.4.1

for details when a class is supposed to be initialized, see http://docs.oracle.com/javase/specs/#12.4.1

粗略地说,应该在以下情况下初始化一个类

Roughly speaking, a class is supposed to be initialized when either


  • 创建第一个实例

  • 调用 static 方法

  • 使用了非最终的静态字段

  • 一些更复杂的极端情况

  • the first instance is created
  • a static method is invoked
  • a non-final static field is used
  • some more complex corner cases

从本质上讲,当您第一次使用任何实际类时,不仅要引用它。

So essentially, when you first use anything of the actual class, not just reference it.

但是例如 Example.bananas = 1; 也应该在没有实例的情况下触发类的初始化。

But e.g. Example.bananas = 1; should also trigger class initialization, without having an instance.

这篇关于“ .class”获取类的方式-是否初始化类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆