Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package question

 
Bob Beerbower
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going through Bruce Eckels TIJ4 and ran across something that confuses me. Bruce has a utility package with a print method that takes the place of

The Print.class and Print.java files are in the net/util directory. The import statement that Bruce uses is

I would have thought that it should be

Or

Print is not a folder, it is a file in /net/mindview
Bruce's import statements compile, mine do not. I had though that by using

that I would have access to all the classes in the /net/minview package. Or that if I only wanted the Print class, I'd use

Why is it necessary to state

[ March 04, 2006: Message edited by: Bob Beerbower ]
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import statements are not recursive. It looks for files inside that package(folder), not inside nested packages.

import static net.mindview.util.Print.*;

This implies that Print is a folder not a file. Is Print supposed to be a file in the util folder? If it is a file, adding .* should generate an error.
[ March 04, 2006: Message edited by: Rusty Shackleford ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rusty's statement is correct, but it doesn't apply here. You're both thinking of plain old "import", which from the sound of it, Bob understands just fine. This is import static, a new feature introduced in Tiger (Java 5, JDK 1.5). "import static" lets you import the static members of a class into a file, so that within that source file, you can refer to them as if they were defined in the current class. By using "import static ... Print.*", Eckel is making the print() methods inside his Print class available all by their lonesome; because of this import, you can just call "print()" instead of "Print.print()".

If it's not compiling for you, you must be using a pre-Tiger JDK -- i.e., JDK 1.4 or earlier. You probably want to upgrade if you want to follow the examples in this new book!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bob Beerbower:
...Bruce's import statements compile, mine do not...

As Ernest pointed out, the static import...
import static net.mindview.util.Print.*;
...works with an unqualified call of the static method...
print("A String.");

But the regular imports...
import net.mindview.util.*;
...or...
import net.mindview.util.Print;
...require a qualified call...
Print.print("A String.");

So... If the static import code is compiling, but the regular import code is not, my guess is that your method calls are simply not specifying the class "Print" in conjunction with the regular imports.


(PS: I just got Eckel's new 4th edition as well. When I saw your post, I downloaded the code to test this. I put it under my java/library directory because that's where my system classpath is pointing -- or so I thought! I've been using this machine for months, and never noticed that my system classpath was set incorrectly. Shows how much I use that setting.)
[ March 04, 2006: Message edited by: marc weber ]
 
Bob Beerbower
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, Thanks for explaining it as clearly as you did. I suspected that it had something to do with the static import being handled differently then a normal import. You gave me a very clear explanation of why this is so.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic