Recursive import Java

后端 未结 4 1202
無奈伤痛
無奈伤痛 2021-01-18 18:24

I am a newbie to Java development. I have a quick question regarding recursive imports in Java.

Lets assume Package \"pkg\" contains the following

  • clas
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-18 18:44

    There is no restriction on imports as you described in your original question (prior to the edit).

    Let's assume you have the following Hierarchy:

    pkg1
        A.java
    pkg1.pkg2
        B.java
    pkg1.pkg3
        C.java
        D.java
    

    You are permitted to import any of the A.java, B.java, C.java, D.java, or combinations therein.

    import pkg1.A;
    import pkg1.pkg2.B;
    import pkg1.pkg3.C;
    import pkg1.pkg3.D;
    

    The only caveat is that within class C, you can use class D without importing D explicitly because they share the same package.

    However, a blanket statement like import pkg1.* will not pull in the classes located further down the package hierarchy; you'd need to import each sub-package as well: import pkg1.pkg2.*, etc. This is simply how the language is designed.

提交回复
热议问题