20

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".

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
1
  • 4
    It would be cool to have a minimal self-contained example for people like me that didn't know that JLabel extends Component. Commented Mar 23, 2015 at 13:59

2 Answers 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.

3
  • I always thought i made myself independt from refactorings in the class hierarchy. You lose the possibility of overriding the value in the subclass.
    – keuleJ
    Commented Sep 18, 2013 at 9:15
  • 2
    You cannot override class members. You can hide them, though, which would be an undisputed bad practice. Note that the API in question clearly states that the method you are calling accepts the constants as defined in the Component class. Commented Sep 18, 2013 at 9:19
  • Even if you could override them it would still be a bad idea to reference idirectly. Hypothatically speaking, if you want the value from Component as access it via JLabel and some other developer suddenly overrides it in JLabel, you would not get the value you intended to. Even worse this might happen without you noticing. Commented Sep 18, 2013 at 9:35
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.

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.

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
  • So the value of the constant is actually compiled into the bytecode?
    – keuleJ
    Commented Sep 18, 2013 at 9:27
  • 1
    +1 And actually, since we're talking float constants here, it wouldn't even reference the class; its compile-time value would be hardcoded in the referencing class. Commented Sep 18, 2013 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.