Is it possible to reference Java types with a partially qualified name? If so, how?
The scenario: I frequently find myself with a data class (e.g. Activity) which needs a view. My standard practice has been to name this class ActivityView, which works, but this view class invariably ends up in a tld.organization.project.views namespace, where the “View” suffix is entirely redundant.
I’d like to remove “View” suffix (so the types would be tld.organization.project.Activity and tld.organization.project.views.Activity), but this means I must use the namespace to qualify the types when I reference them in the same class. Using the namespace to qualify type references is not a bad thing in and of itself, but repeating the fully qualified name of either type is repetitious and difficult to read.
Referencing a partially qualified type (something like ~.Activity or ~.views.Activity) would remove that cruft. Some kind of type aliasing would answer, but it appears Java doesn’t support such functionality. Are there any alternatives?
No, you can’t do that with packages in Java. The closest you could get would be to organize things into nested hierarchies of classes instead of packages. Between that and strategic static importing, you could get the desired effect, though it would be a terribly messy solution. For example:
and:
which can then be referred to as:
I’d suggest that the problems you’re having with names may be pointing to a design problem that needs to be sorted out.
P.S. If I ever had to work on a project that organized classes so, I might have to shoot myself.
P.P.S. Actually, I’d probably try to shoot you first.