This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

import statement  RSS feed

 
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...
 
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 .
 
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.
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!