• Post Reply Bookmark Topic Watch Topic
  • New Topic

implicit/ explicit Java API importation  RSS feed

 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have known that all classes in java.lang.* package is used simply, that mean you do not to explicitly import any classes in java.lang.* package in head of Java file (maybe below package statement).

Is there any package like java.lang.* package (without explicitly import but still use simply)?

Can anyone answer my question?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Yes and No. You can use only what is imported (in case the class of interest is laying down in the other package) or you can use without importing if the class you are going to use appear to be in the same package.

As you correctly mentioned, java.lang package content is implicitly imported, so you don't need to import by yourself. But that is it as of now, it may change in a future, don't know.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler goes through the current package first, then the java.lang package, then the packages named as imports, and the classloaders do the same.
That means it is potentially dangerous to create a class with the same name as a java.lang class (e.g. Character, Exception). Any use of fully qualified names takes precedence over imports. All types not named with their fully qualified names not in the current package and not in java.lang must be imported somehow.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!