在Singleton程序中使用和流动静态语句 [英] Use and flow of Static statements in Singleton Program

查看:118
本文介绍了在Singleton程序中使用和流动静态语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Singleton模式有很多问题。但在这里我想知道的输出可能还包括静态如何在Java中工作。

I know there are lot of questions on Singleton pattern. But here what I would like to know about the output which might also cover how "static" works in Java.

public class Singleton {
    private static Singleton currentSingleton = new Singleton();

    public static Singleton getSingleton() { 
        return currentSingleton;
    }

    private Singleton() {
        System.out.println("Singleton private constructor...");
    }

    public static void main(String[] args) {
        System.out.println("Main method...");
    }

}

这是运行代码的输出...

This is the output from running the code...


Singleton私有构造函数...
主要方法......

Singleton private constructor...
Main method...

当我调试此代码时,控件首先进入行
System.out.println(Singleton private构造函数...)并打印。
(此时私有静态变量currentSingleton仍为null)

然后它转到行

private static Singleton currentSingleton = new Singleton() ;
然后初始化私有变量。
最后,它转到main()方法并打印。

When I debugged this code, control went first to line System.out.println("Singleton private constructor...") and prints. (private static variable currentSingleton is still null at this point)
Then it goes to line
private static Singleton currentSingleton = new Singleton(); and then initializes the private variable. Then at last, it goes to main() method and prints.

我的问题是:


  1. 为什么它首先打印Singleton私有构造函数...,这是私有构造函数。
    我认为控制首先应该转到main()方法,因为它是入口点。此外,我没有在任何地方创建任何实例(除了在变量初始化中)。

  2. 稍后它转到静态变量实例化行(此时currentSingleton = null)

    private static Singleton currentSingleton = new Singleton();
    虽然currentSingleton在这里得到一个值,为什么不再调用构造函数?

主要是我想知道这个程序的控制流程。

Mainly I want to know the flow of control of this program.

推荐答案

在正确初始化之前,您无法调用类中的main方法(即已评估静态字段和静态块)。初始化时,通过调用私有构造函数创建单例的实例。稍后调用main方法。

You cannot invoke the main method in the class until it has been properly initialized (i.e. static fields and static blocks have been evaluated). When it is initialized, an instance of your singleton is created by invoking the private constructor. Later the main method is invoked.

有问题的类有一个静态字段,您可以为其赋值。由于该字段是静态的,因此必须先初始化该类,然后才能在任何上下文中使用该类,也就是说,它必须接收一个值。在这种情况下,它的值恰好是同一个类的实例。这是在类初始化期间触发私有costructor的原因。

The class in question has a static field to which you assing a value. Since the field is static it must be initialized before the class can be used in any context, that is, it must receive a value. In this case its value happens to be an instance of the same class. This is what triggers your private costructor during class initialization.

如果你想深入研究这个过程并更好地理解它,请参考 Java Laguage规范。更具体地说,在第12.4节初始化您可以找到更多详细信息的类和接口

If you want to delve into the process and understand it better please refer to the Java Laguage Specification. More specifically in the section12.4 Initialization of Classes and Interfaces you will find further details.

这篇关于在Singleton程序中使用和流动静态语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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