• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we import empty Packages?  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can we import empty Packages?
For instance i try to import Pack1.*; and it compiles as long as there are some class files in Pack1. But if i delete the class files in Pack1 & try to import Pack1 in some java program, the java prog fails to compile. Can someone explain the behaviour?

Example depicted below:

--Root -- JavaTest program
|_____Pack1 -- Prog2.class
|_______Pack3 -- Prog3.class

If JavaTest.java has import Pack1.*, it compiles as long as i have Prog2.class in Pack1. But if i remove Prog2.class, JavaTest.java fails to compile. Why is this so?
 
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to import an empty package?
[ January 03, 2007: Message edited by: Carol Enderlin ]
 
Joe Joseph
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... I could easily do away with import of Empty Packages.
But rather, would someone explain why the import fails?
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer: the compiler must do it because it seems to be required by the Java Language Specification (JLS):

It is a compile-time error for a type-import-on-demand declaration to name a type or package that is not accessible.


Why? I would guess because the authors of the JLS thought -- and rightly so, I'd say -- that an import statement that imports no classes is likely a typing mistake. There's no reason to have such a statement, and it's a chance for the compiler to catch and report an easy-to-understand error, so it makes good sense to do things that way.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use import something.* in the first place?



Where should I look for MyClass? In the current package? one? two? three? four? five? Bah, I give up, find it yourself
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!