• Post Reply Bookmark Topic Watch Topic
  • New Topic

what happens when i include a package more than once  RSS feed

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!