Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

questions on import  RSS feed

 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see ppl do this: (a)
import java.awt.*;

and this: (b)
import java.awt.Container;

I thought by using (a) we are able to import all the classes under awt? Y do we need (b) then?
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

FIRST: welcome to Javaranch!

SECOND: a moderator will soon ask you to adjust your name to the policy of Javaranch (First Name, Last Name)

THIRD: the first is an implicit import statement, the second is an explicit import statement, another type of import statement is when you import classes from the current package.
The point is that the compiler resolves import statements in this order:
Explicit imports are resolved first, then the classes from the current package, and last the implicit imports (from K&B page 109, last sentence)

Some IDE like Eclipse help you to organize import statements importing only those classes you really need, it make the code a little bit longer but clearer, and the explicit imports are resolved first!

Is it clear now?

Giovanni
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks for the reply.

so it's always more advisable to have explicit imports rather than implicit imports, am i right?

and also...i see these classes..

java.lang
java.lang.ref
java.lang.reflect

if i import java.lang.*, it will automatically imports ref * reflect?
[ May 09, 2005: Message edited by: JY ]
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, if reflect is a Class...
there is something called Java naming convention, an import statements can be:

//implicit import statement
import packageName.*;
import packageName.subPackageName.*;

//explicit import statement
import packageName.ClassName;
import packageName.subPackageName.ClassName;

packages don't start with a capital letter (java, lang, net, io...), classes do start with a capital letter (Object, ArrayList...Reflect?)

I ALWAYS use explicit import statement, but for me it's veeeery easy because I let Eclipse do that work for me! :-)

P.S. you still have to change your display name in your profile!

Giovanni
[ May 09, 2005: Message edited by: Giovanni De Stefano ]
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang
is automatically imported. You don't have to import any of the classes in it.

Edit: I apologize for the confusing reply above. While it is in fact true, it is also true that java.lang.ref & java.lang.reflect are their own packages and not classes inside of java.lang. I originally just read "If I import java.lang" and was a bit hasty on the reply. Thanks to the bartendar for clearing it up.

[ May 09, 2005: Message edited by: Hentay Duke ]
[ May 09, 2005: Message edited by: Hentay Duke ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by J Beginner:
Hey, thanks for the reply.

so it's always more advisable to have explicit imports rather than implicit imports, am i right?

and also...i see these classes..

java.lang
java.lang.ref
java.lang.reflect

if i import java.lang.*, it will automatically imports ref * reflect?


J Beginner:

Thank you for changing your display name, but I'm afaid that it's still not quite right. Please read the JavaRanch Naming policy and change your display name to comply. (We are looking for separate first and last names that are not obviously fictitious; we encourage use of your real name, if possible).

That said, importing java.lang.* does not automatically import java.lang.reflect.* While the designers have the concept of a package hierarchy, Java does not; that is, there is no special relationship between java.lang and java.lang.reflect.

Explicit imports (I find) are much easier to go back and read than implicit imports, especially for third-party and custom source. (You could get away with the .* imports for the standard APIs, but if you are debugging someone else's code and come across a class named MyCustomClass, it's nice to be able to look at the imports and see that that in the com.mycompany.mypackage3 package, because the import read
com.mycompany.mypackage3.MyCustomClass. Had it read
import com.mycompany.mypackage1.*
import com.mycompany.mypackage2.*
import com.mycompany.mypackage3.*

it would be annoying to have to manually determine which package MyCustomClass is in.
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all, thanks to all the replies.. and ya, i've changed my name.. sorry for not putting my real name as normally when register to forums, we used nickname. (should really read before register next time! )

oh well, a simple question ends up with not so simple explanation.

that means when importing.. must be really clear with wat are the classes for and wat is in the classes..
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jolie!!

should you not know what a class does before using it anyway??

and i agree with all above that it is good practice to use explicit imports. in fact, in most production scenarios(read most IT companies), explicit imports are made the standard while writing code and implicit imports are not allowed!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!