• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what happens when i include a package more than once

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#import java.util.Vector;
#import java.util.Vector;

class Test
{
.....
....
..
}

what is happens if i import a class for more than once???
will the compiler internally includes the class 2 times>
is there anything related to performance problem?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"import" (which has no "#" attached to it) just tells the compiler what class you're referring to if you use a given unqualified name; i.e., if you import java.util.Vector, then the compiler knows that if you just say "Vector", you mean "java.util.Vector." That's all it does. The compiler does not, in any way, shape, or form, 'include' the imported class, even once, let alone twice.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ernest says, import is only a compiler directive, it tells the compiler in which package a class is that you are using. It does not work the same as #include in C and C++, which effectively inserts the contents of the file you are using at the point where you put the #include.
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No issues...
JVM will internally load the class only once no matter how many times you import the same class.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To extend on what has already been said, you don't even need the import statement in Java - you could simply type the fully qualified name, that is java.util.Vector, everywhere you want to use a Vector.

The import statement is just syntactic sugar, saving you some characters to type - it doesn't cause any changes in the byte code of the class.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then what happens if i use import java.util.*;
will the compiler tells the jvm to load all the classes wven though if i am not using all of them in my program
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will the compiler tells the jvm to load all the classes wven though if i am not using all of them in my program

No Saikrishna, as a number of people already answered, 'import' is only a hint for the compiler, and importing a whole package (import java.util.*) just tells the compiler: "if you see a class that you don't know, look in the java.util package for it".

The 'import' statement is only used at compile time. It does not have any impact at runtime. It does not have anything to do with classloading.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://forum.java.sun.com/thread.jspa?threadID=277140&messageID=1085598
 
reply
    Bookmark Topic Watch Topic
  • New Topic