8

I have a package com.test.mythingimport. Ideally, I want this to be called com.test.mything.import. Are you able to name things with import or will it cause conflicts?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
John Lippson
  • 1,269
  • 5
  • 17
  • 36

2 Answers2

16

From JLS §7.4.1

A package declaration in a compilation unit specifies the name (§6.2) of the package to which the compilation unit belongs.

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

where Identifier is defined in JLS §3.8 as

Identifier:
 IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral 

So a package name can specifically not be a keyword (such as import), a Boolean (true or false) or null.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • 1
    "So a package name can specifically not be a keyword (such as import), a Boolean (true or false) or null.". It is not right. `IdentifierChars` matter in the naming rules. And for example `foo-` is not valid as identifier. – davidxxx Jul 27 '18 at 21:12
  • Also that there is a `{ PackageModifier }` is quite interesting – Lino Jul 27 '18 at 21:35
  • @Lino Yeah. Turns out it can be only an annotation. [See here](https://stackoverflow.com/questions/25644292/whats-the-point-of-package-modifiers#25644403). – MC Emperor Jul 27 '18 at 22:03
  • how about 'native'? is it reserved by kotlin / android studio or java? didn't figure it out but it's also not allowed apparently *edit: nvm it's under 3.9. Keywords thanks – Kibotu Feb 10 '22 at 08:09
-1

Package names are driven by the convention of file structure in Java. If you want your package to be named com.test.mything.import, your package needs to be in the /com/test/mything/import directory (where the root is the root of your source code for the project).

nasukkin
  • 2,460
  • 1
  • 12
  • 19
  • 1
    If you have the import keyword in the package name, you won't be able to import members of that package. – Coder-Man Jul 27 '18 at 21:04