• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

best approach for import stmts????

 
Reshma Das
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which Stmt is better ?
1. import java.awt.*;
2. import java.awt.classA;
import java.awt.classB
Approach 1 reduces the code length. Class loader brings in the needed class at runtime then why should we unnecessarily give all the class names in import stmt ?
Approach 2 gives readability.
What do u guys think is the best approach ?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a good question. I always liked the .*; version, but I wonder if that makes the compiled code big. I can't spell blooted, bloted.
I actually went through my code at the end and removed all the .*; and put the actual class. Lukily my code didn't use a lot of different classes to be imported, so they were never more than 10.
But I don't think that they can dock you points for it. If they do, I think that is lame.
Mark
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have
import java.awt.*; it does not make your class any bigger than importing java.awt.<someclass>;
The compiler does not actually incliude the imported classes or packages in your class.
 
Andre Mermegas
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read that .* imports can bloat and I've read that they dont..My IDE Intellij Idea, takes care of cleaning up and optimizing imports for me thankfully =)
 
Kalichar Rangantittu
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When would I use * and use specefic import:
If for example, I am using many classes from the same package it is pain to write import suncertify.db.Data; import suncertify.db.DataInfo; import suncertify.db.FieldInfo blah blah right?
In that case it is easier to use import suncertify.db.*.
On an other scenario, if I am using java.util.List and I dont really care about the java.awt.List I would only import those classes I intend to use from java.awt. Its a design convinience so that later I wouldnt have to specify java.util.List list = new java.util.List();
Also depends on your code, if your code only utilizes one or two classes from suncertify.db for example, it is more easy to import just those two so that the reader knows what you did.
As mentioned earlier import * does not increase the class size. Hope this helps.
 
Andre Mermegas
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, it depends on how many classes you plan to use in a package if 3 or more i say use .* if less than 3 use specific imports.
Like I said before, I've read that using .* doesnt make a difference AND I've also read that it does make a difference..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic