Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 589
Jigar Avatar asked Jul 05 '10 11:07

Jigar


People also ask

What is the naming convention for Java packages?

Naming ConventionsPackage 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 names have hyphens?

Just drop the hyphen. The package name doesn't need to match the website name at all. It is more important that there's consistency among packages produced by the company so they all use the same base package name. It is also used to avoid namespace collisions in libraries.

Should Java package names be CamelCase?

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

Can Java package names have underscore?

In Java you can use underscore ( _ ) in your package names and it conforms to Java naming conventions.


6 Answers

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.

like image 74
bragboy Avatar answered Oct 04 '22 06:10

bragboy


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

  • java.sun.com - Code Conventions/Naming

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

  • JLS 6.1 Package Names
like image 26
polygenelubricants Avatar answered Oct 04 '22 08:10

polygenelubricants


Anyone can use underscore _ (its Okay)

No one should use hypen - (its Bad practice)

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)

like image 27
Himanshu Mori Avatar answered Oct 04 '22 08:10

Himanshu Mori


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 to replace hyphonation 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.

like image 44
Andreas Dolk Avatar answered Oct 04 '22 06:10

Andreas Dolk


Underscores look ugly in package names. For what is 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.

like image 20
jpangamarca Avatar answered Oct 04 '22 08:10

jpangamarca


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

like image 21
CraUmm Avatar answered Oct 04 '22 08:10

CraUmm