Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Java - invalid package name - Reserved words in package name

I am in the middle of an android project and was trying to create a new package in it.

com.mycompany.myprojectname.new

Well, Eclipse is not letting me to create it and is showing this error:

Invalid package name. 'new' is not a valid Java identifier

I never knew package name has reserved words, which we cannot use. My questions are;

  1. Is this an Eclipse thing? or a Java thing? I tried a pure Java project (not Android), just to check, but there also I got the same error.
  2. What are other reserved words that is not allowed?
  3. Is there any documentation about this?
like image 295
Krishnabhadra Avatar asked Nov 07 '12 11:11

Krishnabhadra


People also ask

What should be the package name in Eclipse?

The packages for the open-source Eclipse project are all subpackages of org. eclipse. The first package name segment after org. eclipse is generally the project name, followed by the component name.

What is a valid Java package name?

Package names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .

Can Java package name contains hyphen?

You are right, the simple answer is: You are NOT allowed to use the hypen in package names. If you have a unique package name build from a domainname containing a hyphen you have to translate it to the underscore. Reading all this stuff, this somehow feels outdated.

Can package name have underscore?

Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example, com. example.


1 Answers

Yes, this is a general Java thing.

The list of reserved words can be found here. They are:

abstract  continue    for         new         switch
assert    default     goto        package     synchronized
boolean   do          if          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

Documentation on the fact that reserved words can not be used in package names if found in the package naming tutorial, among other places.

The authoritative source is (as always) the Java Language Specification, specifically:

  • § 3.9 Keywords and
  • § 3.8 Identifiers

    An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.

like image 183
Joachim Sauer Avatar answered Oct 14 '22 10:10

Joachim Sauer