在类方法中看到这一行,我的第一 react 是 mock 编写它的开发人员。但后来,我想我应该首先确保我是对的。
public void dataViewActivated(DataViewEvent e) {
if (this != null)
// Do some work
}
那条线会评估为假吗?
最佳答案
不,它不能。如果你使用 this
,那么你就在实例中,所以 this
不为空。
JLS 说:
When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed.
如果你从一个对象调用了一个方法,那么该对象存在或者你之前会有一个 NullPointerException
(或者它是一个静态方法但是你不能使用 this
)。
资源:
关于java - "this"在 Java 中可以为空吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3789528/