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:
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
Yes, it will work.
Yes there is. You would need to implement your own Classloader and play some games to be able to access both during runtime.
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.
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 .
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With