1

I was trying to understand the general hierarhcy of Java packages.
If you consult this Oracle page, it seems that there are 3 main packages: java, javax and org.
All packages, in Java platform 7, start with one of these three.
Are these ones part of a bigger package?
I'd love to see a complete tree, but the one offered by Oracle is very confusing, because it's too detailed.

Another thing I did not undestand is why packages like "java.awt" and "java.awt.color", or "java.lang" and "java.lang.annotation", are put by Oracle on the same level.
I did read online that if you import a "parent-package", you don't import any classes form the "child-packages", and that makes sense. However, it's hard to think that there isn't a relation between java.awt and java.awt.color; It seems intuitive that the least is contained in the fromer.
So, given my current understanding, I would draw a tree that look like this: enter image description here

  • 5
    "However, it's hard to think that there isn't a relation between java.lang and java.lang.color" - there really isn't as far as the language is concerned. The only hierarchy is imposed by human understanding. – Jon Skeet Jan 22 '18 at 10:36
  • @JonSkeet I didn't understand what you are trying to suggest me :( – Gabriele Scarlatti Jan 22 '18 at 10:50
  • 1
    @GabrieleScarlatti The relation is within the intend, not within the language. The developers of `java.awt` needed a representation for colors, so they wrote a package `colors` for awt. Thus, they put `color` as sub-package it in `java.awt`. – Turing85 Jan 22 '18 at 10:54
  • 1
    I'm trying to say that whether it's hard to think it or not, there really *isn't* a relationship between different packages, as far as the language is concerned. – Jon Skeet Jan 22 '18 at 10:55
  • @Turing85: I can't edit the comment now - but everything apart from the name still applies :) – Jon Skeet Jan 22 '18 at 10:56
  • Hard to say, but I think, it was because `java.awt.color` was primary used for `awt` - first java graphics, which came. After some time it was "renewed" by `javax.swing` - notice there is no colors in swing anymore :) So i guess it is from historical reasons. – xxxvodnikxxx Jan 22 '18 at 11:05
  • 1
    Read related question: https://stackoverflow.com/questions/727844/javax-vs-java-package. Also see the Java9 module dependency structure as it is related to jdk packages: https://dzone.com/articles/java-9-modularity-jigsaw – tkruse Jan 23 '18 at 10:25
  • Packages technically have no hierarchy, each packages is a thing on its own (it is not 'nested' in a parent package). So there is no package `java`, and for example `java.util.concurrent` is not a part of package `java.util`. – Mark Rotteveel Jan 23 '18 at 10:52
  • @MarkRotteveel http://www.docjar.com/docs/api/java/util/package-index.html It looks like sometimes it's included – Gabriele Scarlatti Jan 23 '18 at 16:01
  • That is just that sites interpretation, it is not how it actually works. – Mark Rotteveel Jan 23 '18 at 17:05

1 Answers1

2

No, there is no "bigger" package. The structure evolved over time, with different ideas and marketing influencing the naming.

You do not import packages in java, just classes. So before Java9, it does not really matter much even in which package a class resides except for the package-protected visibility scope (which is not used that much), and the lack of need to import classes from the same package.

Often classes from a subpackage are used by classes in a parent package, but not the other way round. But there is no strict rule about this.

tkruse
  • 10,222
  • 7
  • 53
  • 80