Introduction to JVM Languages
上QQ阅读APP看书,第一时间看更新

Choosing a package name

There are a few rules that package names must follow:

  • A package name can contain dots; in fact, dots are used to separate elements in a name.
  • An element in a package name can contain letters, digits, and an underscore.
  • Each element in the package name must start with a letter, not a digit. Digits are only allowed after a letter.
  • An element in the package name cannot be the same as a keyword reserved by the Java language. This includes the names of primitive types (such as int, short) and built-in keywords (for example class, for, or final).

There are a few universal naming conventions that most major projects adhere to. It is strongly recommended that you follow these for each package in all your projects:

  • The complete package name should be in lowercase.
  • Start your package name with the reversed Internet URL of either:
    • Your company's site
    • Your project's site URL
    • Your project's public source code repository URL
    • Your personal home page or blog URL
  • You can come up with your own conventions to differentiate classes and prevent naming conflicts between projects. You could add a department name, office region, or project name to your package name.
  • If you have an illegal element in your package name, put an underscore before or after the conflicting element to make it legal and/or replace the illegal character with an underscore.