Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is from Sun's guide when they released the feature (emphasis in original):</p> <blockquote> <p>So when should you use static import? <strong>Very sparingly!</strong> Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). ... If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually.</p> </blockquote> <p>(<a href="https://docs.oracle.com/javase/8/docs/technotes/guides/language/static-import.html" rel="noreferrer">https://docs.oracle.com/javase/8/docs/technotes/guides/language/static-import.html</a>)</p> <p>There are two parts I want to call out specifically:</p> <ul> <li>Use static imports <strong>only</strong> when you were tempted to "abuse inheritance". In this case, would you have been tempted to have BusinessObject <code>extend some.package.DA</code>? If so, static imports may be a cleaner way of handling this. If you never would have dreamed of extending <code>some.package.DA</code>, then this is probably a poor use of static imports. Don't use it just to save a few characters when typing.</li> <li><strong>Import individual members.</strong> Say <code>import static some.package.DA.save</code> instead of <code>DA.*</code>. That will make it much easier to find where this imported method is coming from. </li> </ul> <p>Personally, I have used this language feature <em>very</em> rarely, and almost always only with constants or enums, never with methods. The trade-off, for me, is almost never worth it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload