• Post Reply Bookmark Topic Watch Topic
  • New Topic

import statement  RSS feed

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aloha !

if i write


error: com.lakme.product is already defined in a single-type import
import com.nivia.product;

but if i write



it works ! why ? how both classes are imported here with same name ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:
but if i write



it works ! why ? how both classes are imported here with same name ?


Does it work? If you also try to instantiate a product object (without using the fully qualified name), does it compile? And which product object do you expect to get?

Henry
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
meeta gaur wrote:
but if i write



it works ! why ? how both classes are imported here with same name ?


Does it work? If you also try to instantiate a product object (without using the fully qualified name), does it compile? And which product object do you expect to get?

Henry


No, it wont work if i do instantiate any object.I have to use fully qualified name for that.but i was talking about only import statements.If i do import them explicitly then compiler doesn't allow to compile but if i use * for them then it allows and runs also(no object instantiation). Is it mean both classes have been imported there with * import statements ? if they are imported there as there is no compile and run time error then again why i have to use fully qualified name to instantiate object ? then what is meaning of those * import statements.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:
No, it wont work if i do instantiate any object.I have to use fully qualified name for that.but i was talking about only import statements.If i do import them explicitly then compiler doesn't allow to compile but if i use * for them then it allows and runs also(no object instantiation). Is it mean both classes have been imported there with * import statements ? if they are imported there as there is no compile and run time error then again why i have to use fully qualified name to instantiate object ? then what is meaning of those * import statements.



That is simply how it works...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5

The first case follows the single-type import rules, and the second case follows the import-on-demand rules. And there is no "run time" here -- importing is merely to tell the compiler what you mean, when you specify a class without the fully-qualified name. It is to help you (as the programmer) to not have to fully-qualify everything.

Henry
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!