简体   繁体   English

Java “this”-在 eclipse 内容辅助中标记为 static 的关键字

[英]Java “this”-keyword marked as static in eclipse content assist

Does anybody know the reason why the this keyword (Java) is marked as static final in the content assist in Eclipse?有谁知道this关键字(Java)在Eclipse的内容辅助中static final的原因? final makes sense to me, but why static ? final对我来说很有意义,但为什么static

截图 Eclipse

The screenshot was made with Eclipse 2020-03, but I'm observing this behavior since many years.屏幕截图是使用 Eclipse 2020-03 制作的,但多年来我一直在观察这种行为。

The code doing this is in org.eclipse.jdt.internal.ui.text.java.ParameterGuesser执行此操作的代码位于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$
  }
}

The key thing in that code is该代码中的关键是

JavaElementImageDescriptor.FINAL | JavaElementImageDescriptor.STATIC

as the flags to JavaElementImageDescriptor which is hard coding the display of the static and final overlay images.作为JavaElementImageDescriptor的标志,它硬编码 static 和最终覆盖图像的显示。 So these are always displayed for this .所以这些总是this显示。

As to why that was chosen the code doesn't give any reason.至于为什么选择它,代码没有给出任何理由。

Two reasons.两个原因。 1) this can not be reassigned and 2) you're inside a constructor and not a normal method. 1) this不能被重新分配和2)你在一个构造函数中,而不是一个普通的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM