Note that there are some explanatory texts on larger screens.

plurals
  1. POimport static without package name
    primarykey
    data
    text
    <p>Consider the following simple example of code:</p> <pre class="lang-java prettyprint-override"><code>public class TestStaticImport { static enum Branches { APPLE, IBM } public static void doSomething(Branches branch) { if (branch == APPLE) { System.out.println("Apple"); } } } </code></pre> <p>If we will try to compile this code, we will get the error message:</p> <pre class="lang-java prettyprint-override"><code>java: cannot find symbol symbol: variable APPLE location: class TestStaticImport </code></pre> <p>This could be solved by introducing static import of this <code>enum</code>:</p> <p><code>import static</code> ... <code>TestStaticImport.Branches.*</code></p> <p><strong>But in this moment incomprehensible things (for me) begin:</strong></p> <p>this solution works fine, everything is well compiled, until class <code>TestStaticImport</code> will be moved into empty root package, i.e. there isn't any </p> <p><code>package blablabla;</code> in the top of this java file;</p> <p>Code line: <code>import static TestStaticImport.Branches.*;</code> is highlighted as valid code in my Intellij IDEA (name of IDE doesn't matter, just for information), but when I try to compile such code following error appears:</p> <p><code>java: package TestStaticImport does not exist</code></p> <p>So, there are actually two questions:</p> <p>1) <strong>Main question</strong>: why is it impossible to <code>import static</code> from empty directory?</p> <p>2) What is another way (if it exists) for allowing in code references to enum's fields using just their names (i.e. <code>APPLE</code> instead of <code>Branches.APPLE</code>), except static import?</p> <hr> <p>P.S. Please, don't tell me, that empty packages is ugly style and so on. This question is just theoretical problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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