• 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

Coding convention for import statement

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
After taking a seven month break from my SCJD due to studies I have returned to complete it. Following this break I will start off with a nice (very) easy question!!
When importing files what is the correct convention i.e. is the package name followed by a star e.g. import java.util.* OR is it an absolute path e.g. import java.util.ArrayList? If you are importing one or two classes you would use the absolute and if using many use the star�is this the general consensus? The only reason I ask is that going through my code I have been inconsistent with this e.g. using * sometimes when just using one class and other times importing the absolute class.
Thanks in advance,
John
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi John:
I don't think that there is an import statement convention. However, good coding style is to import only what you need. So, one should always use an absolute path instead of '*' to import classes.
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,
Welcome back!
The best practice in that area is to import specific classes instead of whole packages (using the '*').
Now most of Java editors (Eclipse, JBuilder do it for sure) let you "optimize" your importa statements, by replacing generic imports (.*) by specific ones, and by removing useless imports.
Regards,
Phil.
 
John Canavan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hanna and Philippe for the quick reply!

Will use the fully qualified import statements from now on.
Regards,
John
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the book, I think I remember if it is like a few classes, like 2, the it is better to use fully qualified so that ppl dont have a hard time figuring out the object type you use is from which package. If you have 30 classes from that package, the it is better to use .*; cos 1, it makes your code look messy, 2 you are likely to keep adding it if you dont use it. There is no adv or disadv over one another except for clarity of your code.
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anna,

From the book,(...)


Which one?
Regards,
Phil.
 
Anna Hays
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Max's or Kathy & Bate's... that's where I get the ideas from. Not sure if you can get the exact wording...
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anna,

I cannot check in Max's book because I sent my copy to a rancher a few months ago. But Kathy & Bert seem to prefer explicit class imports (see p. 109).

Regards,

Phil.
[ May 13, 2004: Message edited by: Philippe Maquet ]
 
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have always followed explicit import of the individual classes and that too in alphabetical order...
 
reply
    Bookmark Topic Watch Topic
  • New Topic