Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding convention for import statement  RSS feed

 
John Canavan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • 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.
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Anna Hays
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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 ]
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always followed explicit import of the individual classes and that too in alphabetical order...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!