File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Classpath and import statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Classpath and import statement " Watch "Classpath and import statement " New topic

Classpath and import statement

ghanshyam malpani

Joined: Jul 14, 2008
Posts: 1
if we set all the classes into classpath then what is significant of import statement ?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

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:
<pre name="code" class="core">

public class MyClass {

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

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.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 692
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.*;
I agree. Here's the link:
subject: Classpath and import statement
It's not a secret anymore!