11

When I try to create a package implements using Intellij (community edition) I got message Not a valid package name. Is this because of the keyword being used?

enter image description here

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
Bala
  • 11,068
  • 19
  • 67
  • 120
  • 3
    [Java Package Naming Conventions](http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html). This may provide some insight. – takendarkk Apr 23 '14 at 17:22
  • No it can not be package name..package name must not be any java keyword like int, class,implements too – Karibasappa G C Apr 23 '14 at 17:23

4 Answers4

26

Is this because of the keyword being used?

Yes, a package name has the following form

PackageDeclaration:
    {PackageModifier} package Identifier {. Identifier} ;

where Identifier is

Identifier:
    IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral
IdentifierChars:
    JavaLetter {JavaLetterOrDigit}
JavaLetter:
    any Unicode character that is a "Java letter"
JavaLetterOrDigit:
    any Unicode character that is a "Java letter-or-digit"

So keywords cannot be used.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    Out of curiosity, do you know any examples of a "PackageModifier"? The JLS says it has to be an annotation, but I've never seen such a use before. – asteri Apr 23 '14 at 17:29
  • @JeffGohlke It's not very common but you can annotate packages. Think of the `package-info.java` file. See [here](http://stackoverflow.com/questions/7501494/what-is-jaxb-generated-package-info-java) for a JAXB example. – Sotirios Delimanolis Apr 23 '14 at 17:30
  • @JeffGohlke http://stackoverflow.com/questions/2099431/whats-the-point-of-package-annotations – yshavit Apr 23 '14 at 17:30
  • Interesting. Thanks for the examples! – asteri Apr 23 '14 at 17:31
4

You can not use a java keyword in your package declaration.

abstract   continue   for          new         switch
assert     default    if           package     synchronized
boolean    do         goto         private     this
break      double     implements   protected   throw
byte       else       import       public      throws
case       enum       instanceof   return      transient
catch      extends    int          short       try
char       final      interface    static      void
class      finally    long         strictfp    volatile
const      float      native       super       while

These keyworkds can not be used.

the syntax of package declaration is

PackageDeclaration:
{PackageModifier} package Identifier {. Identifier} ;

Here Identifiers are any Unicode character that is a "Java letter" or any Unicode character that is a "Java letter-or-digit".
The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

Refer

  1. http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
  2. http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-IdentifierChars
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
2

You cannot use a Java keyword as package name. See JLS on Names and Identifiers

Adrian Pang
  • 1,125
  • 6
  • 12
-1

This works fine for me, in Intellij 13.1.2, however, you can't use a package with this name even if you create one, because the java package statement won't accept a keyword anywhere in the package names. So, I can create, but can't use:

package com.implements.thing;
Software Engineer
  • 15,457
  • 7
  • 74
  • 102