Recursive import Java

后端 未结 4 1220
無奈伤痛
無奈伤痛 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 19:09

    It sounds like you're asking why import java.awt.* doesn't also import java.awt.color.* and so on.

    This is best explained by this tutorial where it says

    At first, packages appear to be hierarchical, but they are not. For example, the Java API includes a java.awt package, a java.awt.color package, a java.awt.font package, and many others that begin with java.awt. However, the java.awt.color package, the java.awt.font package, and other java.awt.xxxx packages are not included in the java.awt package. The prefix java.awt (the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.

    More importantly,

    Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color, java.awt.font, or any other java.awt.xxxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:

    import java.awt.*;
    import java.awt.color.*;
    

提交回复
热议问题