• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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: 4764
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.*;
 
"To do good, you actually have to do something." -- Yvon Chouinard
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic