Explain Codes LogoExplain Codes Logo

What is the convention for word separator in Java package names?

java
package-naming-conventions
java-best-practices
java-standards
Alex KataevbyAlex Kataev·Jan 12, 2025
TLDR

Java package names use continuous lowercase letters with no separators, such as underscores or camelCase. When writing a package name based on a URL, use reverse-domain naming. For example, write org.wikipedia.pagecontent instead of org.wikipedia.page_content or org.Wikipedia.PageContent. Remember, in Java, strict adherence to name conventions ensures clean, readable code.

Rules of Engagement

Navigating Java package name conventions can be akin to negotiating a linguistic minefield. Here are the key conventions:

  • Lowercase letters: Using lowercase letters avoids confusion with class names, which typically use CamelCase.
  • Dot Separator: Separate components of the package hierarchy using dots.
  • No Special Characters: Don't use underscores and hyphens to separate words. If your package name includes domain names with hyphens, replace the hyphen with an underscore.
  • Avoid Keywords: If you absolutely must use a Java keyword as part of your package name, prefix or suffix it with an underscore.
  • Consider Readability: Your package name should succinctly describe its contents in an easily comprehensible manner.
// Welcome to the Java package lowlands package com.stackoverflow.this_is_not_the_package_you_are_looking_for;

Exceptions are NOT exceptional

Even in the world of Java package naming conventions, certain exceptions and idiosyncrasies exist. It's crucial to bear them in mind:

  • Special Characters in Domain Names: Is there a hyphen in your domain name? Replace it with an underscore when adopting it as a package name.
  • Java Keywords: Dress up Java keywords with an underscore prefix or suffix to prevent conflicts.
  • Capital Offenses: Avoid uppercase letters in package names to keep them distinct from class names.
  • Domain Name Prefixes: Adopting domain-based prefixes for package names not only mirrors domain ownership but also should be in lowercase to adhere to Java standards.

Code Gymnastics

Here are a few best practices that could steer you through potential package naming storms:

  • Tailor-made Guidelines: Sometimes, organizations have unique conventions. As long as these largely comply with Java standards, they're kosher!
  • Documentation: Use package-info.java to comment about the package purpose and content — it's like leaving a trail of breadcrumbs for fellow developers!
  • Usability Matters: Think about the package consumers - your fellow developers! Is your package name self-explanatory, intuitive even?
  • Look & Feel: Balance length against clarity to achieve optimal readability and flow.

Reality Check

For added context, here are real-world examples of Java package naming conventions in the wild:

  • Apache Commons Lang: Consistent adherence to Java standards is evident in org.apache.commons.lang3.
  • Google's Guava library: The package name com.google.common.collect flows with ease and meets all Java guidelines.

To get a sense of how these principles translate into practice, poke around these libraries!