Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

what does the the "import" statement do for the program on earth?

Thread view: 
Friday - 15 Dec 2006 07:33 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.