此代码会导致运行时错误吗?(使用了一些 android 库,但我不认为这是特定于平台的)
class A
{
Context sContext;
public A()
{
//initialize sContext here
}
public static Conext getContext()
{
return sContext;
}
}
class B
{
public static Context anotherContext;
static
{
anotherContext = A.getContext();
}
}
令人困惑的部分是在使用 eclipse 调试器时,
A.getContext()
计算为非空值。
然而
anotherContext
评估为 null
有人对这种行为有任何想法吗?谢谢
编辑 :
我的错, sContext 是一个静态变量,但它仅在实例方法中被赋值,如
public void onCreate()
{
sContext = getApplicationContext();
}
那么在这种情况下,行为会是什么?