import static without package name

只谈情不闲聊 提交于 2020-01-01 08:35:07

问题


Consider the following simple example of code:

public class TestStaticImport {
    static enum Branches {
        APPLE,
        IBM
    }
    public static void doSomething(Branches branch) {
        if (branch == APPLE) {
            System.out.println("Apple");
        }
    }
}

If we will try to compile this code, we will get the error message:

java: cannot find symbol
  symbol:   variable APPLE
  location: class TestStaticImport

This could be solved by introducing static import of this enum:

import static ... TestStaticImport.Branches.*

But in this moment incomprehensible things (for me) begin:

this solution works fine, everything is well compiled, until class TestStaticImport will be moved into empty root package, i.e. there isn't any

package blablabla; in the top of this java file;

Code line: import static TestStaticImport.Branches.*; 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:

java: package TestStaticImport does not exist

So, there are actually two questions:

1) Main question: why is it impossible to import static from empty directory?

2) What is another way (if it exists) for allowing in code references to enum's fields using just their names (i.e. APPLE instead of Branches.APPLE), except static import?


P.S. Please, don't tell me, that empty packages is ugly style and so on. This question is just theoretical problem.


回答1:


The Java language specification forbids any imports from the unnamed package:

A type in an unnamed package (§7.4.2) has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported, and (b) static members of types in an unnamed package cannot be imported. As such, §7.5.1, §7.5.2, §7.5.3, and §7.5.4 all require a compile-time error on any attempt to import a type (or static member thereof) in an unnamed package.




回答2:


In ancient times, the Java inventors had to map Java types to files so the compiler could do some real work. They decided to map packages to folders and types to files. That worked pretty well. It especially set the emotional background for newcomers: "I hate you. Don't mess with me." But I digress.

The default package is a problem, though, since it doesn't have a well defined folder. If you have package com, you know that there is a folder com somewhere but what's the name of the folder for the default package?

So the designers decided that import and default package don't mix. In fact, you get an error when you try to import anything that has no package (i.e. import TestStaticImport without the static and * would also fail). See How to import a class from default package

So the problem isn't the static import but that you try to import from the default package.

Like some other corner cases in Java, there is no solution.

see also: In Java- "Static Members of the default package cannot be imported"- Can some one explain this statement?



来源:https://stackoverflow.com/questions/14258369/import-static-without-package-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!