I often look at internet examples of Java code where your class organization is always contained in packages with prefix br.com.algumPartName. Why is "br.com" used?
I often look at internet examples of Java code where your class organization is always contained in packages with prefix br.com.algumPartName. Why is "br.com" used?
Sun, the company that created the language and the Java platform, created a naming convention to prevent name collisions.
Suppose you have created a library to handle geolocation. Your program has a package called GPS
. The package has several classes and one of them is called Coordenada
.
Now suppose I have created a library to defer GPS signal reading. I create a package called GPS
, which has several classes. One of my classes in the package is called Coordenada
.
If someone includes our two packages in the same application, what happens? How will the application know which code GPS.Coordenada
refers to, your package or mine?
Sun's suggestion was to use the domain name of the company holding the code as a prefix of the package name to prevent name collisions ... For it is impossible for two companies to have the exact same domain. Reversing the domain is just an aesthetic decision.
The documentation for this convention can be found on the Oracle website, the current owner of Java. Follow the link:
Brazilian companies follow the pattern and place the br
stretch up front.
.NET has the same problem of name collision risk since its inception, but Microsoft personnel have solved it differently. You can give nicknames to namespaces to avoid collisions. That's why in .NET libraries do not usually use domain names, but only company names.
Finally, what to do when the code is not a company, but something personal, or at least something or someone who has no domain? Well, in that case, * in your * the documentation says nothing. Just as a company is free not to follow the convention, in that case you are free to name your packages as you see fit.