544

How should one separate words in package names? Which of the following are correct?

  1. com.stackoverflow.my_package (Snake Case using underscore)
  2. com.stackoverflow.my-package (Kebab Case using hyphens)
  3. com.stackoverflow.myPackage (Camel Case)
  4. com.stackoverflow.MyPackage (Pascal Case)

What is the general standard?

3
  • 32
    another example not yet mentioned is using a period: com.stackoverflow.my.package
    – Brad Cupit
    Commented May 30, 2013 at 19:29
  • 25
    (2) isn't legal Java. Unclear why you're even asking about it.
    – user207421
    Commented Nov 21, 2017 at 11:02
  • Note that all this is just to ensure uniqueness. Only thing actually enforced is to stay out of the java.* space. Commented Mar 21, 2020 at 10:45

6 Answers 6

440

All three are not the conventions.

Use com.stackoverflow.mypackage.

The package names do not follow camel casing or underscores or hyphens package naming convention.

Also, Google Java Style Guide specifies exactly the same (i.e. com.stackoverflow.mypackage) convention:

5.2.1 Package names

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

Google Java Style Guide: 5.2 Rules by identifier type: 5.2.1 Package names.

7
  • 11
    I partially agree - they are not 'wrong' according to the java naming conventions but they shouldn't be used in my opinion. (java.sun.com/docs/codeconv/html/CodeConventions.doc8.html) Commented Jul 5, 2010 at 12:00
  • 1
    @Andreas_D the link you provided states that "The prefix of a unique package name is always written in all-lowercase ASCII letters" Commented Oct 26, 2015 at 20:12
  • 4
    I have been following this convention. However, the readability really isn't very good. What is the reason behind all lower cases?
    – Haomin
    Commented Apr 19, 2021 at 16:45
  • 9
    Using underscore is suggested in the Java package naming convention page, linked in the answer. Eg: "org.example.hyphenated_name" Commented Nov 12, 2021 at 15:36
  • 5
    @Haomin : the reason is that packages use the underlying file system, which might be case-sensitive or case-unsensitive. In the case of multi-developers, multi-platform project, it might lead to problems for folder names.
    – khaemuaset
    Commented Aug 25, 2022 at 9:22
307

Here's what the official naming conventions document prescribes:

Packages

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

Examples

  • com.sun.eng
  • com.apple.quicktime.v2
  • edu.cmu.cs.bovik.cheese

References


Note that in particular, anything following the top-level domain prefix isn't specified by the above document. The JLS also agrees with this by giving the following examples:

  • com.sun.sunsoft.DOE
  • gov.whitehouse.socks.mousefinder
  • com.JavaSoft.jag.Oak
  • org.npr.pledge.driver
  • uk.ac.city.rugby.game

The following excerpt is also relevant:

In some cases, the internet domain name may not be a valid package name. Here are some suggested conventions for dealing with these situations:

  • If the domain name contains a hyphen, or any other special character not allowed in an identifier, convert it into an underscore.
  • If any of the resulting package name components are keywords then append underscore to them.
  • If any of the resulting package name components start with a digit, or any other character that is not allowed as an initial character of an identifier, have an underscore prefixed to the component.

References

4
38

Anyone can use underscore _ (its Okay)

No one should use hypen - (its Bad practice and it may result you in error as per @glglgl in comments section)

No one should use capital letters inside package names (Bad practice)

NOTE: Here "Bad Practice" is meant for technically you are allowed to use that, but conventionally its not in good manners to write.

Source: Naming a Package(docs.oracle)

7
  • 112
    Yes, using a hyphen is bad practice, because it is an error. And writing code that doesn't compile is indeed bad practice.
    – glglgl
    Commented May 26, 2017 at 7:01
  • Good link - helps give all this some context when you know what the source says. I'm used to all lower case convention as well. But according to the docs, looks like it is simply a matter of choice/style . I have added a comment to the specific post asked about camel case for package names (which I don't think is a duplicate of this post , btw - which just asks about the convention in general) stackoverflow.com/questions/36755783/…
    – Gene Bo
    Commented Oct 15, 2018 at 20:58
  • "No capital letters", although I would agree that all caps or something that looks like a ClassName is a bad idea, it also eliminates thisExample. Saying "it's bad practice" is about the most unconvincing, vague and meaningless reason I can think of. Can this be elaborated on? (i.e. define 'bad practice')
    – Manius
    Commented Jun 4, 2019 at 20:45
  • You're still basically saying "it's bad" without giving justification why it should be considered bad. Does it break tooling? Create confusion? Is it harder to read or type? For the examples given I think we can answer yes to many of those. But I can't understand an absolute ban on capital letters. A package name LikeThis (like a class name) is obviously confusing, but likeThis isn't confusing to me and seems more readable for a two word package name like bigdataSource (vs "bigdatasource"). Unless there's some reason camelCase is a bad idea for packages that I'm unaware of, that seems fine.
    – Manius
    Commented Jun 12, 2019 at 16:31
  • Turns out I missed this: oracle.com/technetwork/java/codeconventions-135099.html All lowercase is part of Oracle's package name convention. I still think that's a rather crappy convention to rule out camel case which starts lower, for those (rare) times when you need to use a two word package name and it doesn't make sense to make it two directories. But oh well.
    – Manius
    Commented Jun 12, 2019 at 16:37
20

The official naming conventions aren't that strict, they don't even 'forbid' camel case notation except for prefix (com in your example).

But I personally would avoid upper case letters and hyphenations, even numbers. I'd choose com.stackoverflow.mypackage like Bragboy suggested too.

(hyphenations '-' are not legal in package names)

EDIT

Interesting - the language specification has something to say about naming conventions too.

In Chapter 7.7 Unique Package Names we see examples with package names that consist of upper case letters (so CamelCase notation would be OK) and they suggest replacing hyphenation by an underscore ("mary-lou" -> "mary_lou") and prefix java keywords with an underscore ("com.example.enum" -> "com.example._enum")

Some more examples for upper case letters in package names can be found in chapter 6.8.1 Package Names.

2
  • 2
    As Andreas has noted, there's no rules about using upper casing in package names. One specific reason to avoid it is that I have seen people run into problems with mixed case package names when doing cross platform development. Especially when someone decides to rename or change the case of a package, you're then relying on both your VCS and development environments to do exactly the right thing with the directory case.
    – Shorn
    Commented Oct 1, 2014 at 1:52
  • 2
    Actually, there are rules: "The prefix of a unique package name is always written in all-lowercase ASCII letters" (oracle.com/technetwork/java/codeconventions-135099.html) Commented Oct 26, 2015 at 20:11
7

Underscores look ugly in package names. For what it's worth, in case of names compound of three or more words, I use initials (for example: com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo) and then document the package purpose in package-info.java.

3
  • 4
    this might not be readable and difficult to understand the contents of package by just looking at the package name Commented Feb 21, 2019 at 5:56
  • 2
    Fair enough. That's why I suggest using documentation. I'd use this approach any time instead of full words concatenated (apiratesheet - is that 'API Rate Sheet' or 'A Pirate Sheet'?) Commented Feb 27, 2019 at 15:56
  • 1
    it is just your flavor. i have two services with names like poirot_a, poirot_b. i don't like package poriot.a, casuse smb may want to place stuff into poirot package which may have no sense. Commented Mar 4, 2022 at 8:50
2

Concatenation of words in the package name is something most developers don't do.

You can use something like.

com.stackoverflow.mypackage

Refer JLS Name Declaration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.