我正在研究一个问题,以便在彼此之间存储两个类的引用
例如:
class A {
B b;
A(B b){
this.b = b;}
}
class B {
A a;
B(A a){
this.a = a;}
}
public static void main(String...s){
A a = new A(new B(null));
a.b.a = a;
}
现在,如果我使用以下语句而不是上面的初始化:
A a = new A(new B(a));
我得到了以下错误,这很明显:
Main.java:19: error: variable a might not have been initialised
A a = new A(new B(a));
但是如果我在JShell上尝试相同,它就可以正常工作(只是为了确保变量a从未被初始化,我在执行语句之前检查了变量a,确认它之前没有初始化:
可能是我在这里遗漏了一些东西,但有些人可以帮助我理解为什么在JAVA中执行同一语句有两种不同的行为.
理解这个问题的一个简单方法是在Jshell中允许使用below语句,但在正常程序中不允许:
var somevar = somevar;
解决方法:
声明A a =新A(新B(a));不是局部变量的声明.
但首先,您所描述的问题可以简化为:
jshell> int a = a;
a ==> 0
现在,这是怎么回事?
好吧,正如JEP 222的Snippets所说:
In JShell, a “variable” is a storage location and has an associated type. A variable is created explicitly with a FieldDeclaration snippet:
06001
or implicitly by an expression (see below). Variables have a small amount of field semantics/syntax (for example, the
volatile
modifier is allowed). However, variables have no user-visible class enclosing them and will be generally viewed and used like local variables.
所以,它们的行为有点像字段,有点像局部变量.
与字段类似,但与局部变量不同,没有初始化程序的声明将指定默认值,例如
jshell> int b;
b ==> 0
但是,回到int a = a;. JEP 222第State条规定:
The JShell state is held in an instance of JShell. A snippet is evaluated in a JShell with the
eval(...)
method, producing an error, declaring code, or executing a statement or expression. In the case of a variable with an initializer, both declaration and execution occur.
因此,变量的声明和初始化程序的执行是两个单独的操作.
这意味着当执行初始化程序时,变量已经被声明,并且已经被赋值为默认值.
实际上,int a = a;被评估为:
jshell> int a;
a ==> 0
jshell> a = a;
a ==> 0
这就是jshell REPL的设计方式.这不是一个bug.