Java - Can't import com.package.*(wildcard)

后端 未结 2 1777
难免孤独
难免孤独 2021-01-19 07:01

This may be a simple issue, but Google has returned nothing.

I have read Help with packages in java - import does not work

I still don\'t understand why a di

2条回答
  •  無奈伤痛
    2021-01-19 07:53

    It sounds like you're trying to import a package that contains no classes, but only contains sub-packages.

    i.e. You have classes in com.company.functions - e.g. com.company.functions.Foo
    But no classes directly in com.company - e.g. com.company.Bar

    Despite that fact that java packages appear to be hierarchical, for the purposes of imports, they aren't.

    So, you cannot import com.company.* because it contains no classes of its own.
    You can import com.company.functions.* because it does contain classes
    And you can import com.company.functions.Foo because that is a class.

    Now, you might be tempted create a Bar class in com.company. That would allow you to import com.company.*
    But because imports are not treated hierarchically, that wouldn't cause classes in com.company.functions to be imported
    You would still need to explictly import com.company.functions.Foo, or wildcard import com.company.functions.*

提交回复
热议问题