Java - 对于A类中定义的B类(作为静态),这是什么意思?

Evg*_*eny 2 java

这是一个更具体的例子:

public class A {
    public static class B {
             public void f() {
                 synchronized (B.this) {
                     // do something
                 }
             }
     }
}
Run Code Online (Sandbox Code Playgroud)

B.this指的是什么?

Ern*_*ill 5

It's the active instance ofB . If the class weren't static, you'd also haveA.this`可用,引用包含A的实例.

因为类是静态的,所以永远不需要使用它B.this,因为它永远this不会是模棱两可的.