【发布时间】:2021-02-09 20:55:39
【问题描述】:
假设我面前有以下代码:
public class Theory {
public static void main(String[] args) {
new Theory().program();
}
void program(){
A.a = A.b; //won't compile
A.a = new A().b;
A.b = A.a; // won't compile either
new A().b = A.a
}
static class A{
static int a;
int b;
}
}
当我将鼠标悬停在代码上时,它显示“无法从静态上下文引用非静态字段”,我有点理解,但我无法理解为什么连续行不显示编译器错误?
【问题讨论】:
-
没有名为
A.b的东西。b是非静态的,所以你可以得到一个的唯一方法是someInstanceOfACreatedWithNew . b换句话说......请研究它实际上在你添加 static时的含义> 到内部类的定义。
标签: java variables static initialization