Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

package question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic