Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it discouraged to access static members indirectly?

Tags:

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
like image 429
keuleJ Avatar asked Sep 18 '13 09:09

keuleJ


1 Answers

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.

like image 149
Marko Topolnik Avatar answered Oct 21 '22 17:10

Marko Topolnik