为什么不鼓励间接访问静态成员?

时间:2021-11-11 20:59:38

Why is it discouraged to access static members indirectly? In Eclipse, you can en-/disable this warning under Preferences > Java > Compiler > Error/Warnings > "Indirect access to static member".

为什么不鼓励间接访问静态成员?在Eclipse中,您可以在首选项> Java>编译器>错误/警告>“间接访问静态成员”下启用/禁用此警告。

Example when "Indirect access to static member" is configured to cause a warning:

“间接访问静态成员”配置为引发警告时的示例:

JLabel label = new JLabel();
label.setAlignmentX(JLabel.CENTER_ALIGNMENT);       // causes warning    
label.setAlignmentX(Component.CENTER_ALIGNMENT);    // is ok

2 个解决方案

#1


8  

In addition to what was said by Marko Topolnik, there is the fact that if the JLabel was to have a static member by the same name in the future, the mere recompilation of the JLabel.java would not make the calling code aware of the constant existing in that module. Instead until recompilation of the code, it would still happily use the Component.CENTER_ALIGNMENT despite the source code stating contrarily.

除了Marko Topolnik所说的内容之外,如果JLabel将来要使用相同名称的静态成员,那么仅仅重新编译JLabel.java就不会使调用代码知道常量存在于该模块中。相反,直到重新编译代码,尽管源代码相反,它仍然会愉快地使用Component.CENTER_ALIGNMENT。

The latter reference is sure to refer to the static member of Component by the name CENTER_ALIGNMENT, whereas the former would refer to the member in either Component or in any subclass up to JLabel if the name is redefined in any of them later on.

后一个引用肯定会通过名称CENTER_ALIGNMENT来引用Component的静态成员,而前者将引用Component中的成员或任何子类中的成员,直到JLabel,如果稍后在其中的任何一个中重新定义名称。

In case of using a static function or member variable, if a later version of the subclass also declares an overridden one, the meaning of the indirect access would change even without recompilation. All of this might be a bit hypothetical, but good to consider anyway.

在使用静态函数或成员变量的情况下,如果子类的更高版本也声明了重写,则即使没有重新编译,间接访问的含义也会改变。所有这些可能都有点假设,但无论如何都要考虑好。

#2


19  

Accessing a member of the Component class through JLabel gives a false impression that this member is specific to JLabel, where in fact it is Component's member and just happens to be inherited by all its subclasses. Nothig is lost by accessing it through the declaring class, and there is definitely something won in clarity.

通过JLabel访问Component类的成员会给人一种错误的印象,即该成员特定于JLabel,实际上它是Component的成员,恰好被其所有子类继承。 Nothig通过宣告课程来访问它,并且肯定有一些清晰的东西。

#1


8  

In addition to what was said by Marko Topolnik, there is the fact that if the JLabel was to have a static member by the same name in the future, the mere recompilation of the JLabel.java would not make the calling code aware of the constant existing in that module. Instead until recompilation of the code, it would still happily use the Component.CENTER_ALIGNMENT despite the source code stating contrarily.

除了Marko Topolnik所说的内容之外,如果JLabel将来要使用相同名称的静态成员,那么仅仅重新编译JLabel.java就不会使调用代码知道常量存在于该模块中。相反,直到重新编译代码,尽管源代码相反,它仍然会愉快地使用Component.CENTER_ALIGNMENT。

The latter reference is sure to refer to the static member of Component by the name CENTER_ALIGNMENT, whereas the former would refer to the member in either Component or in any subclass up to JLabel if the name is redefined in any of them later on.

后一个引用肯定会通过名称CENTER_ALIGNMENT来引用Component的静态成员,而前者将引用Component中的成员或任何子类中的成员,直到JLabel,如果稍后在其中的任何一个中重新定义名称。

In case of using a static function or member variable, if a later version of the subclass also declares an overridden one, the meaning of the indirect access would change even without recompilation. All of this might be a bit hypothetical, but good to consider anyway.

在使用静态函数或成员变量的情况下,如果子类的更高版本也声明了重写,则即使没有重新编译,间接访问的含义也会改变。所有这些可能都有点假设,但无论如何都要考虑好。

#2


19  

Accessing a member of the Component class through JLabel gives a false impression that this member is specific to JLabel, where in fact it is Component's member and just happens to be inherited by all its subclasses. Nothig is lost by accessing it through the declaring class, and there is definitely something won in clarity.

通过JLabel访问Component类的成员会给人一种错误的印象,即该成员特定于JLabel,实际上它是Component的成员,恰好被其所有子类继承。 Nothig通过宣告课程来访问它,并且肯定有一些清晰的东西。