5

(Disclaimer: I am new to Java and also, I have read the related SO question.)

I have the following code:

import org.apache.pdfbox.pdmodel.*;
...
PDFont font = PDType1Font.HELVETICA_BOLD;

But the PDFont class is not recognized in Eclipse.

When I add the following:

import org.apache.pdfbox.pdmodel.font.PDFont;

The PDFont class is picked up.

Given that the PDFont class is located under the hierarchy specified in the first import statement ending with the asterisk, why is a specific import statement necessary?

Also, is there a way to search for the location of a class in a library if one doesn't have the documentation handy?

Community
  • 1
  • 1
Sabuncu
  • 5,095
  • 5
  • 55
  • 89

1 Answers1

12

The on demand import declarations are not recursive

A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.

Your PDFont type is in package

org.apache.pdfbox.pdmodel.font

but you've tried to import

 import org.apache.pdfbox.pdmodel.*;

the package org.apache.pdfbox.pdmodel does not contain a type named PDFont.

So, alternatively, you could have used

 import org.apache.pdfbox.pdmodel.font.*;

Also, is there a way to search for the location of a class in a library if one doesn't have the documentation handy?

If the library is published as a .jar, you can unzip it and search through it.

Most IDEs typically have a feature to search types by their simple name. For example, in Eclipse, you can use CTRL (cmd) + SHIFT + T and type the simple or fully qualified name to search for (if it's on the classpath).

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I see, it only picks up what's at that level (pdmodel) and does not recurse. Thanks so much, will accept when SO let's me. – Sabuncu Dec 26 '14 at 18:03
  • @Sabuncu It's important to note that there is no inherent hierarchy in `package` names. It's just easier for developers to "nest" them like that. – Sotirios Delimanolis Dec 26 '14 at 18:06
  • Got it, thank you. What about my second (minor) question? ;-) – Sabuncu Dec 26 '14 at 18:07
  • Unbelievable! Can't thank you enough, I wish I could upvote more. (Yahoo is very lucky!) – Sabuncu Dec 26 '14 at 18:11
  • 2
    Isn't it better to use the IDE to import the necessary classes and also let the IDE remove them when there are no dependencies left? I can't seem understand why it would make much sense to import all classes in a directory when using an IDE. – Joop Dec 26 '14 at 18:17
  • @Sabuncu As Joop is suggesting, you can also use, again in Eclipse, `CTRL + SHIFT + o` to let your IDE manage your `import` statements. It will clean up anything unnecessary. – Sotirios Delimanolis Dec 26 '14 at 18:18
  • @Joop As I mentioned, I am new to Java, and the code is not mine, but from a tutorial. When I modified the tutorial to add new code, the problem surfaced. – Sabuncu Dec 26 '14 at 18:18
  • Yes, just tried `CTRL + SHIFT + o` and it filled in everything for me! What is the name of this shortcut (i.e. its location in the menu structure)? – Sabuncu Dec 26 '14 at 18:20
  • @Sabuncu Something like "Organize imports". – Sotirios Delimanolis Dec 26 '14 at 18:21
  • @Joop In my opinion, it is not better to have the IDE automatically handle import statements. Many times it saves some effort, but when it doesn't save effort you get into a fight with your IDE's auto-completion abilities. I'm for an IDE than can do it if requested, just don't ever have it cleaning up imports without a command. Otherwise, you can inadvertently get the wrong imports for classes which have similar names but different packages, and then to fix that watch yourself killing an import statement just to see yet another wrong one come into play. – Edwin Buck Jan 10 '15 at 03:23
  • @EdwinBuck Good to know that auto-importing does not always happens correctly. I have yet to experience that though, but good to know that it could. When I said automatic I meant semi-automatic so I didn't mean that the IDE would insert it without asking or suggesting an import, or using a shortcut. Thank you for your perspective. – Joop Jan 10 '15 at 07:53