Why should java package name be lowercase?

43,759

Solution 1

Directly from Oracle Docs

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

Solution 2

But it's interesting why java specification don't allow uppercase characters letters in package and cause write something like this:

The specification allows it just fine. It's only a convention to use all-lower-case.

As gtgaxiola says, this does avoid conflict with type names... in .NET naming conventions this does happen, leading to advice that you do not name a class the same as its namespace. Of course, using camelCase packages would avoid the collision entirely.

I suspect reality is that it wasn't thoroughly considered when creating the package naming conventions. Personally I rarely find it to be a problem - if I end up seeing a package with one element of "remotefilesystemsynchronization" then the capitalization isn't the main thing I'd be concerned about :)

Solution 3

Its just another convention - One may ask why class name always has to start with Capital or method name starts with small case and then camel-cased. Java doesn't forces you to use that way. Its just that a set of underlined rules helps huge community as Java developers to write easily understandable code for the majority who follow conventions.

No definite reason can be assigned as such. Its just what felt good and was in practice by then majority programmers while writing the convention. But yes guidelines would definitely be there before writing conventions. I don't mean its a whimsical work. Guidelines are made so that just by looking at the various elements we should be able to tell if its class, method or package - and via following conventions it has been achieved for so long now.

Share:
43,759
Oleksii Kolotylenko
Author by

Oleksii Kolotylenko

Updated on April 18, 2021

Comments

  • Oleksii Kolotylenko
    Oleksii Kolotylenko about 3 years

    Actually this is completely theoretic question. But it's interesting why java specification don't allow uppercase characters letters in package and cause write something like this:

    com.mycompany.projname.core.remotefilesystemsynchronization.*
    

    instead of

    com.myCompanyName.projName.core.remoteFileSystemSynchronization.*
    
  • gtgaxiola
    gtgaxiola over 11 years
    True @Jon! same goes for Class Names starting with Upper Case
  • Will
    Will over 11 years
    Package names are also used in conjunction with filesystem directories, which often have restrictions on character case themselves.
  • Mike76
    Mike76 over 7 years
    Who uses a file system without upper case support? You have either ext3, NTFS or the mac thing, we are not in the nineties anymore
  • Aquarius Power
    Aquarius Power over 7 years
    so basically, a package sub-part name beggining with lower case and further words in upper case (like methods) will have no chance to collide with classes as long they all begin in uppercase right? :), but most importantly, that would be the only problematic collision related to it, as I cant see a package sub-part name colliding with a method name...
  • joro
    joro over 6 years
    "to avoid conflict with the names of classes" -> You cannot confuse com.site.myName with class name because in case of class, it would be com.site.MyName (starting with capital letter). So there is no logic in favour of "myname" approach. I think "myName" is better because it is easier to read. E.g. "com.marvel.movies.gardiansofthegalaxy"
  • tomorrow
    tomorrow almost 6 years
    yes, theoretically. But not practically. It's actually pity that it's just a convention.I came here because I need to change my colleague's code and he uses a package name in the form company.AppName.something.else. Once you allow upper case letters someone will use them the wrong way. If I now wanted to add a class company.AppName, I'd need to rename the whole package - which I'll probably do anyway, sooner or later. ;-) BTW, isn't remoteFileSystemSynchronization a bit too long for a package name anyway?
  • Nedorot
    Nedorot over 5 years
    In your case, AppName isn't lowerCamelCase as @joro indicates (in your case, I would probably strangle the person who created the AppName, just as I wanted to strangle someone in our company who created classes starting in lower cases...). I'm part of the developpers thinking that com.pany.app.tableOfContents is easier to read than com.pany.app.tableofcontents (or com.pany.app.toc). And com.pany.app.table.of.contents is even worse...
  • Brendan Kim
    Brendan Kim about 5 years
    Also, consider the class under such a Capitalized package vs an inner class. Quite confusing, eh? e.g. my.app.Service.Executor
  • Flimtix
    Flimtix almost 2 years