Question

Going by the Java Tutorial for package naming convention if I were to use the following approach:

org.example.hyphenated_name.user

org.example.hyphenated_name.product

How would this translate to actually creating this package structure. Would I create a separate package for org then within org would i create example and so on or would it just be 2 packages org.example.hyphenated_name.user and org.example.hyphenated_name.product

Basically what I'm wondering is if the . is just used for naming separation or if it's used to mean physically creating a new package each time.

Was it helpful?

Solution 2

A package isn't a "thing" that you have to create (except that you normally have to create the directory hierarchy for storing the source files). You pretty much just say that your class belongs to some package, and the compiler says "OK, whatever you want". There's no built-in relationship between package_a.package_b and package_a.package_b.package_c that has any effect on language semantics. (What I mean here is that if you have a class in package_a.package_b.package.c, then within that class there is nothing special about classes in package_a.package_b as opposed to, say, package_z.package_y. In both cases, it's just "some other package".)

OTHER TIPS

This is how it looks physically on a disk drive:

$ mkdir -p org/example/hyphenated/name/user 
$ mkdir -p org/example/hyphenated/name/product
$ tree org
org
└── example
    └── hyphenated
        └── name
            ├── product
            └── user

5 directories, 0 files

So actually, you have a structure of packages:

  • org
  • org.example
  • org.example.hyphenated
  • org.example.hyphenated.name
  • org.example.hyphenated.name.product
  • org.example.hyphenated.name.user

By creating such a structure, you can now use each package for a different action. Product-specific action classes in org.example.hyphenated.name.product and user-specific in org.example.hyphenated.name.user package. This way you can as well put classes that use both of these packages in e.g. org.example.hyphenated package. This way you create a well-build application structure that is easier for other people (and for you in 2-3 months) to read.

EDIT: Normally you won't need to care about creating folders for packages, because your IDE should take care of this instead of you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top