Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath and import statement

 
ghanshyam malpani
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we set all the classes into classpath then what is significant of import statement ?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of a classpath and import statements are different. Import statements don't find classes. They rely on the classloader to do this, and it uses the classpath.

The import statement tells a Java class the fully qualified package name of a class being used. You don't need to use it. For example, you could write a class like this:
<blockquote>code:
<pre name="code" class="core">
package foo.bar;

public class MyClass {

public static void main(String[] args] {
java.util.Date myDate = new java.util.Date(System.currentTimeMillis());
}

}
</pre>
</blockquote>
No import statements, but I've "imported" the class java.util.Date. You can probably see why it is easier to use imports from the example above.
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of a classpath and import statements are different


Not true at compile time(the only time the import has any effect). In fact they compliment each other. The compiler connects the path given in the import statement to the classpath to create the full path to the class definition.
For example: if a class is in package pkg and the class file is located at:
C:\javastuff\project\pkg\PGM.class then the classpath should be set to:
C:\javastuff\project for the package pkg referenced in your program by:
import pkg.*;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic