0

For example, according to the naming convention, package name for the domain; 'cambridge.edu.com', would be 'com.edu.cambridge'.

What's the purpose of creating the 'com' directory and then 'edu' sub-directory and then 'cambridge' sub-directory?

Instead we could make it 'comeducambridge' a single directory and have the classes/interfaces in it?

hamal
  • 55
  • 6
  • 6
    Idontknowmaybethereisagoodreasonperhapsyoucanthinkofone – Raedwald May 20 '21 at 17:26
  • So its just for readability? – hamal May 20 '21 at 17:27
  • This is common in other languages such as C#, dart. – OldProgrammer May 20 '21 at 17:31
  • Biggest to smallest. – stdunbar May 20 '21 at 17:31
  • @stdunbar what's the point of it? Could you be more clear? – hamal May 20 '21 at 17:33
  • 1
    Let's flip it around: why aren't domains written the other way round? – Andy Turner May 20 '21 at 17:37
  • "com" is the biggest - encompassing all `.com` domains. The domain name is next, after being given a namespace by the "com". Within your domain you can subdivide it the way you want. Put another way, why do we have directories on a computer file system? Do you want every file in the O/S in one directory? Ultimately it's to prevent name collisions. You and I can write a class with the same name but, since it's in a different hierarchical package, there are no issues. – stdunbar May 20 '21 at 17:42
  • 3
    Java packages themselves are not hierarchical (as in package `com.example.x.y` is not part of the package `com.example.x`), only the naming convention and its representation on a normal filesystem is hierarchical. – Mark Rotteveel May 20 '21 at 17:54

1 Answers1

0

It is just a naming convention. No one actually cares, as long as you make sure that there is no other class with the name "comeducambridge.MyClass" in the world.

You can call it with just the name of your company, for example "mycoolcompany", if you totally sure that there is no other "mycoolcompany". Even if there is, unless this company writes java code, you will not have any problems, caused by class name conflicts, when some "lucky" user will manage to use two libraries with the same "mycoolcompany" package prefix.

Using "net.mycoolcompany" for me makes no sense, but maybe if you want to make your domain popular, you should do it.

Just don't call your package prefix "java.util", it is already occupied.

Mykhailo Skliar
  • 1,242
  • 1
  • 8
  • 19