Java“ this”关键字在Eclipse内容帮助中标记为静态

Does anybody know the reason why the this keyword (Java) is marked as static final in the content assist in Eclipse? final makes sense to me, but why static?

Screenshot Eclipse

屏幕截图是使用Eclipse 2020-03制作的,但是多年来我一直在观察这种行为。

评论
bquos
bquos

The code doing this is in org.eclipse.jdt.internal.ui.text.java.ParameterGuesser

// add 'this'
if (currentType != null && !(fEnclosingElement instanceof IMethod && Flags.isStatic(((IMethod) fEnclosingElement).getFlags()))) {
  String fullyQualifiedName= currentType.getFullyQualifiedName('.');
  if (fullyQualifiedName.equals(expectedType)) {
    ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_FIELD_PUBLIC, JavaElementImageDescriptor.FINAL | JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE);
    res.add(new Variable(fullyQualifiedName, "this", Variable.LITERALS, false, res.size(), new char[] {'.'}, desc));  //$NON-NLS-1$
  }
}

该代码中的关键是

JavaElementImageDescriptor.FINAL | JavaElementImageDescriptor.STATIC

as the flags to JavaElementImageDescriptor which is hard coding the display of the static and final overlay images. So these are always displayed for this.

至于为什么选择它,代码没有给出任何理由。

点赞
评论
vsit
vsit

Two reasons. 1) this can not be reassigned and 2) you're inside a constructor and not a normal method.

点赞
评论