有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java静态对象初始化

每一次未来内容。future被称为为什么不创建一个新的MyFuture类并将其分配给future

FutureContent.future

public class FutureContent {

        public static MyFuture future = new MyFuture();

}

共 (3) 个答案

  1. # 1 楼答案

    Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory.

    Understanding Instance and Class Members

  2. # 2 楼答案

    正是因为future是一个静态字段aka,一个类变量,这意味着初始化只会发生一次

  3. # 3 楼答案

    你不需要“调用”FutureContent.future,你需要引用它

    它在类加载时初始化一次;不需要再次初始化它。这就是static部分(广义上)的意思