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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With