Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Java class name duplicate package name? [closed]

I'm thinking about what is the best practice in Java for the following case:

I have a logic in my program that I want to put in a separate package.

For example (my current case), it is a logic that will collect some statistics and later send them to server). So my package under my project com.example.project will be named com.example.project.statistics.

There will be some classes inside the package, in this case StatisticsDispatcher (basically container and manager for events) and StatisticsEvent (event that should be calculated into statistics).

The question is, what naming convention should I use? I see two options:

  1. StatisticsDispatcher and StatisticsEvent like mentioned above. The class name makes clear what the class does, but is a bit verbose in the absolute name, like com.example.project.statistics.StatisticsEvent.
  2. Dispatcher and Event. It is shorter and the purpose of the class is clear from its absolute name, like com.example.project.statistics.Dispatcher , but later on in the code these names can be ambiguous with other packages' classes and even if not, when I import these classes in the beginning of a file, class names like "Dispatcher" and "Event" are very general and in the middle of a file it will not be evident what are they related to.

I haven't found any mention about this when googling java package and class naming conventions. I'm afraid the first option will be correct, but I really don't like the verbosity in the package name and the class name, so I want to ask you, other programmers, how do you solve such cases.

Thanks in advance

like image 647
amik Avatar asked Nov 14 '12 11:11

amik


People also ask

Can package name be same as class name?

Yes, it will work.

Can we have two classes with same name under the same package?

Yes there is. You would need to implement your own Classloader and play some games to be able to access both during runtime.

Can we have multiple Java classes with the same name in the same package?

A Java file contains only one public class with a particular name. If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error.

How Should Java packages be named?

Package names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .


1 Answers

I would prefer to be explicit (and perhaps verbose) to being concise (and perhaps ambiguous).

Your example re. Dispatcher / Event is a good one. If you're unlucky, you'll end up having to fully qualify each occurrence of these to resolve any ambiguities.

In such a scenario you'll end up either with a lot of verbose code, or restructuring your code such that conflicting classes don't co-exist (conflicts may actually act as an indicator that unrelated entities are co-existing, but that's another discussion)

like image 109
Brian Agnew Avatar answered Oct 11 '22 23:10

Brian Agnew