0

I'm using InteliJ Idea 14. And for the use of javaDocs I'm trying to create the package-info.java (Not a valid name but it's the requirement), but the IDE won't allow to create a new Java file with that name. It says This is not a valid Java qualified name.

Anyway I could create it as a new File with the name "package-info.java".

Is it okay to do like that ? or is there any other way ?

And why javaDoc asks to create a java file with an invalid name ?

prime
  • 14,464
  • 14
  • 99
  • 131
  • You mean update Intelij-Idea ? – prime Aug 12 '17 at 01:45
  • 1
    But why the down votes ? Appreciate if someone can give a reason – prime Aug 12 '17 at 01:45
  • 1
    Perhaps because you didn't research your Q before asking. If you want to really understand *why* a language feature the way it is, the *first place* to look is the language's specification. This applies to any PL, not just Java. And in this case, the JLS explains *precisely* why it was designed that way. – Stephen C Aug 12 '17 at 02:08
  • The linked dup Q&A answers all of your questions. "Is it OK?", "Are there alternatives?" and "Why?". (The answer to "Why" is to read the rationale in the JLS ... using the link that the top Answer helpfully gives you.) – Stephen C Aug 12 '17 at 02:14
  • @StephenC That package.html question is not asking the same thing and the answer there is not a direct answer to this question. You should have posted an answer here with the link and copy of the JLS "7.4.1. Named Packages" footnote about package-info.java and not marked this as a duplicate. Also, in the future, you could consider any question that can be answered by reading specifications to be a question asking to be pointed to the relevant location in the specs. Sifting through manuals is tough, especially for newbies. If you know where something can be found, be helpful and point it out. – Brandon Essler Dec 14 '18 at 17:34

1 Answers1

3

See documentation: IntelliJ IDEA 2017.2 Help - Creating Packages and package-info Files:

To create a package-info.java file, follow these steps:

  1. Open the Project tool window (e.g. View | Tool Windows | Project).
  2. Select the package for which you want to create a package-info file.
  3. Do one of the following:
    • Select File | New | package-info.java.
    • Select New | package-info.java from the context menu.
    • Press Alt+Insert and select package-info.java.
Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    Yeah. Thats what I did then. Thanks for the tips. But why ? And why javaDoc asks to create a java file with an invalid name ? – prime Aug 12 '17 at 01:40
  • 2
    @prime Because that way it's guaranteed to not clash with a class name. See Java Language Specification [§7.4.1. Named Packages](https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.4.1): *The sole annotated `package` declaration, if it exists, is placed in a source file called `package-info.java` in the directory containing the source files for the package. This file does not contain the source for a class called `package-info.java`; indeed it would be illegal for it to do so, as `package-info` is not a legal identifier.* – Andreas Aug 12 '17 at 01:41