• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Classpath and import statement

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if we set all the classes into classpath then what is significant of import statement ?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5088
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.*;
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic