Static inner classes need import for annotations

半城伤御伤魂 提交于 2019-11-30 21:20:11

This is an interesting one. According to [1], the scope of the name "TestClassOne" is "the entire body of" class "TestSuite".

Is the annotation in "the body of" TestSuite? Apparently not. But that's not very fair. The scope rule was defined before annotation was introduced. I don't see any problem if a class annotation is considered in the scope of the class. They are very intimate anyway.

Another question is how come simple name "TestSuite" can be referenced in the annotation? It turns out the spec covers this one. Annotation is a modifier, which is part of the type declaration, and "The scope of a top level type is all type declarations in the package".

However it is possible that the spec got it right by accident. The rules were defined before annotation was introduced, and remain the same afterwards. So although it covers the case in technicality, it could be an accident. This is not to doubt the brain power of language designers - the whole spec is just too damn complex.

[1] http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3

You don't need to import the inner class, you could access them using

TestSuite.TestClassOne

The details can be found in JLS, but my simple rule is: An import a.b.c.d.e allows you to use e instead of the fully qualified name. It doesn't allow you to use f.

Just an additional info: it's necessary to import inner classes not only when using annotations, but also when using it as a parametric type, which is very annoying.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!