关于静态成员变量的问题 [英] Question about static member variables
问题描述
在下面的代码中,我假设成员变量 mBar
只会在第一次构造 Foo
对象时实例化......并且这个 mBar
实例化将与所有未来的 Foo
对象共享,但 Bar()
构造函数将不会再次被调用.这准确吗?
In the following code, it is my assumption that the member variable mBar
will only be instantiated upon the first construction of a Foo
object... and that this mBar
instantiation will be shared with all future Foo
objects, but the Bar()
constructor will not be called again. Is this accurate?
public class Foo {
private static Bar mBar = new Bar();
public Foo() {
}
推荐答案
该对象实际上可能在创建第一个 Foo 之前以 WAY 的方式构造.当 Classloader 在内存中加载 Foo.class 时会执行它,这可能会发生在任何时候......特别是当你加载其他使用 Foo 类的类时,或者当你调用类的静态方法时......
The object might actually be constructed WAY before creation of first Foo.. It will be executed when Classloader loads the Foo.class in memory and this can happen pretty much at any time.... Specifically when you load other classes that use Foo class, or when you call a static method of the class....
这篇关于关于静态成员变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!