问题标签 [static-initializer]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - C++0x 静态初始化和线程安全
我知道从 C++03 标准开始,函数范围的静态初始化不能保证是线程安全的:
随着 C++0x 标准最终提供标准线程支持,函数范围的静态初始化是否需要线程安全?
java - In what order do static/instance initializer blocks in Java run?
Say a project contains several classes, each of which has a static initializer block. In what order do those blocks run? I know that within a class, such blocks are run in the order they appear in the code. I've read that it's the same across classes, but some sample code I wrote disagrees with that. I used this code:
and got this output:
START
static - grandparent
static - parent
static - child
instance - grandparent
constructor - grandparent
instance - parent
constructor - parent
instance - child
constructor - child
END
The obvious answer from that is that parents' blocks run before their children's, but that could just be a coincidence and doesn't help if two classes aren't in the same hierarchy.
EDIT:
I modified my example code by appending this to LoadTest.java:
As implied by the class name, I never referenced the new class anywhere. The new program produced the same output as the old one.
c - 涉及 {} 的 C 代码
我在一些 C 代码中看到了这一点:
这到底是什么意思?
java - 为什么 Java 不允许从静态初始化块中抛出已检查的异常?
为什么 Java 不允许从静态初始化块中抛出已检查异常?这个设计决定背后的原因是什么?
java - 使用静态初始化块
我知道静态初始化块是如何工作的。
谁能告诉我它的一些典型用途。
java - 获取静态初始化块以在 java 中运行而不加载类
我有几个课程,如此处所示
...
如何更改TrueFalseQuestion
类以便始终运行静态方法,以便在运行 main 方法时得到 1 而不是 0?我不希望 main 方法有任何变化。
我实际上是在尝试实现子类向工厂注册的工厂模式,但我已经简化了这个问题的代码。
.net - 使用 linq2sql 时出现奇怪的异常
如果我实现这样的类,这就是我得到的:
db 是一个 linq2sql 数据上下文
为什么会发生这种情况以及如何解决这个问题?
UPD:该文件由 linq2sql 生成:
如果我在方法内实例化 db(不是这里的属性)一切正常。并且静态方法一直工作到今天早上,但现在即使是 2 天前的版本(从存储库恢复)也出现了同样的错误。
更新 2:
所以这是我解决问题后的部分课程:
c++ - 具有类成员的结构的静态初始化
我有一个用大量 vanillachar*
指针定义的结构,但也是一个对象成员。当我尝试静态初始化这样的结构时,出现编译器错误。
SomeObject
有一个没有参数的公共默认构造函数,所以我不认为这会是一个问题。但是当我尝试编译它(使用 VS)时,我收到以下错误:
知道为什么吗?
更新:这是 SomeObject 的定义
java - Java - 静态初始化块内部的类类型
是否可以从静态初始化块中获取类类型?
这是我目前拥有的简化版本:
这更接近我正在做的事情,即初始化一个包含有关对象及其注释等信息的数据结构......也许我使用了错误的模式?
编辑
我根据最适合我的问题的方法选择了公认的答案,但在我看来,当前所有三个答案都有其优点。
java - 如何强制初始化一个类?
最好和最干净的方法是什么?具体来说,我需要静态初始化块中的一些代码才能在该类中运行,但我想让它看起来尽可能干净。