How is import done in Java?

后端 未结 8 1240
终归单人心
终归单人心 2020-11-29 19:57

For instance

import org.apache.nutch.plugin.Extension,

though used many times,

I\'ve no much idea what is done essentially.

相关标签:
8条回答
  • 2020-11-29 20:38

    Imports are just hints to the compiler telling him how to figure out the full name of classes.

    So if you have "import java.util.*;" and in your code you are doing something like "new ArrayList()", when the compiler processes this expression it first needs to find the fully qualified name of the type ArrayList. It does so by going thru the list of imports and appending ArrayList to each import. Specifically, when it appends ArrayList to java.util it get the FQN java.util.ArrayList. It then looks up this FQN in its class-path. If it finds a class with such a name then it knows that java.util.ArrayList is the correct name.

    0 讨论(0)
  • 2020-11-29 20:39

    Buliding off of Thomas' answer, org.apache.nutch.plugin is a path to the class file(s) you want to import. I'm not sure about this particular package, but generally you'll have a .jar file that you add to your classpath, and your import statement points to the directory "./[classpath]/[jarfile]/org/apache/nutch/plugin"

    0 讨论(0)
提交回复
热议问题