Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import statement

 
Puneet Agarwal
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In import statements , why is it that we must not write import java.util.* but instead we must write the fully qualified import statement for the class(s) which are being used , like java.util.Hashtable . There should be no unused import statements in the file . Why is that so...
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why is it that we must not write import java.util.* but instead we must write the fully qualified import statement for the class(s) which are being used , like java.util.Hashtable


Who says this!!! it is totally legal to import the entire classes in the package (java.util.*) or simply import a single class (java.util.Hastable), both statements are correct.
I for example, like most programmers tend to use the first option because if you want to import every single class by writing its own individual import statement, then you'll end up with a code that has a dozen of import statement.
FYI, importing the entire package does not make your code any less efficient.
 
Puneet Agarwal
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked this question in an interview yesterday . From my programming experience in Java , it is certainly not recommended to import the entire package . I am saying this from the code that I work on in my project .
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all about the standards and your personal preferences.
Normally, if I'm importing more than 4 classes from a same package, I use the wildcard version (import *). Anything less than 4, I import the individual classes.
 
Anand Sidharth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said before importing the entire package instead of the class doesnt affect the performance of the code.
The main reason is to get an idea of the code and what classes are used in the class as soon as u see the code. It gives a clear picture to the person who goes through the code and the reviewer and some times to u when u have a look at the code after a long time on the classed that are used in that code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic