File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes package question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package question" Watch "package question" New topic
Author

package question

Bob Beerbower
Ranch Hand

Joined: Jun 06, 2001
Posts: 32
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 ]

Regards Bob Beerbower
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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 ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Bob Beerbower
Ranch Hand

Joined: Jun 06, 2001
Posts: 32
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package question