Sometimes when I view source java code I notice some files placed in another package besides the default one but have not understood when or why this practice is used. Are there situations where you must or mustn't use separate packages? Anyone care to explain please?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
A package is a mechanism for organizing Java classes into namespaces similar to the modules of Modula.
Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time.
Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.
By providing proper name to the package, will clearly gives an highlevel picture of the usage of the classes present in the perticular package.
Example:
Packages are a good way to group classes that are related in some way. It also provides a mechanism to restrict access to some aspects of a class to only other classes in the same package - which is useful when trying to hide the implementation of a class (a core object-oriented principle).
The most important use of packages is to organize code into modules that are bigger than individual classes.
By default (unless you make them public) fields, methods, and classes are not visible to code outside of the same package. This provides for "information hiding" and enforces de-coupling of your modules (so that they can only communicate via public interfaces and you can change implementation details of each package independently).
Others by language specification
The short answer:
Use packages when your project requires a specific organization or hierarchy to it, or when your framework disallows the use of the default package. For simple CS projects, it can be overkill.
The longer answer:
Packages are folders recognized by Java that allow you certain perks:
You can have two classes that are named the same that live in different folders, without causing conflicts.
A common example is
java.util.Date
andjava.sql.Date
; depending on what you're doing, you may wind up using both. If you do, you'd have to use the fully qualified class name, which is like writingjava.util.Date date = new java.util.Date();
.You give your project a sense of hierarchy and organization; giving each class a sensible place to "live".
Take, for example, my current project. I have decided to write a metadata parser that will read MP3, FLAC, Vorbis, and AAC files.
Right away, I have four common interfaces:
...however, these are all really compression formats (and FLAC is lossless, so there's no compression there), so they belong in a place that conveys that.
That's all good and dandy. But where would the class that actually does the parsing live? What if it lived in a reader package?
Suppose now I want to implement those format interfaces. Doesn't make sense to have them living at the same level as the interfaces themselves, since those are just an API into what the actual object will be anyway. Let's move that to an
impl
package.I'd go on, but it gets kind of crazy from here.
What I have here is a sense of hierarchy and structure to my project. It's a sizable project, so I could benefit from the organization.
We have packages, which are nothing but logical namespaces to distinguish 2 classes with the same name. Second use is to group together common classes performing one task as one namespace(
java.io
,java.sql
).