I am a beginner of Java.
I don't understand what the the "import" statement do for the
program clearly.
For example, I just write a program like this:
import java.util.HashMap
class MyClass
{ ... }
...
In my opinion, the computer first copy all the code of the
"java.util.HashMap" class to
my program,then go down to the next .
Isn't it?
Thanks for your attention.
Andrew Thompson - 15 Dec 2006 07:51 GMT
> I am a beginner of Java.
With usenet as well, it seems.
Please refrain from multi-posting, in future.
(X-post to c.l.j.p/h., w/ f-u to c.l.j.h. only)
Andrew T.
Furious George - 15 Dec 2006 07:55 GMT
> I am a beginner of Java.
> I don't understand what the the "import" statement do for the
[quoted text clipped - 5 lines]
> { ... }
> ...
A simple example should be sufficient to explain
/*use import*/
import java.util.HashMap; public class MyClassWithImport { private
HashMap map /*no need for fully qualified name*/; }
/* no import */
public class MyClassWithoutImport { private java.util.HashMap /*must
use fully qualified name*/ ;}
/*Something to look out for*/
import java.util.List ;
public class MyClassWithDuplicates {
private List someList;
private java.awt.List otherList /*must use fully qualified name or
compiler will make mistake*/; }
> In my opinion, the computer first copy all the code of the
> "java.util.HashMap" class to
> my program,then go down to the next .
> Isn't it?
No. All import does is save keystrokes.
> Thanks for your attention.
Daniel Pitts - 15 Dec 2006 20:23 GMT
> I am a beginner of Java.
> I don't understand what the the "import" statement do for the
[quoted text clipped - 11 lines]
> Isn't it?
> Thanks for your attention.
Actually, what import does is to let the compiler know that class
exists.
import java.util.HashMap;
tells the compiler that when you say "HashMap" later in the source
code, you are refering to the java.util.HashMap class specifically.
It doesn't actually change your program at all, just the source code to
the program.