• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Import * and empty package

 
Sergej Smoljanov
Ranch Hand
Posts: 468
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and

Question: why import java.*; - is allowed there is no compiler error, and import temp.*; - not allowed and get error
:test\Test.java:8: error: package temp does not exist import temp.*;
(while after comment -import temp.*; this import import temp.newpackage.NewClass; - work fine
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no technical reason but a practical one. The Java compiler is written to prevent you from making obvious mistakes or introducing unforeseen errors in the code.
One could write a java compiler that allows importing an empty package and still produce the same byte code because import statements do not affect the byte code. They are just a short cut for humans.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS has a section on the observability of a package. Based on this definition I would say this code should compile successfully (without any error), because the subpackage newpackage of the package temp is observable.

And the order of the imports does matter in this case: Test1 doesn't compile (same compiler error), but Test2 (import of the subpackage before the import of the package) compiles successfully!





Hope it helps!

Disclaimer: I'm pretty sure such question will not be on the exam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic