When to use separate package in java?

2020-07-17 07:49发布

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?

标签: java package
7条回答
smile是对你的礼貌
2楼-- · 2020-07-17 07:55

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:

com.sample.Ejb;
com.sample.EjbImpl;
com.sample.Delegate;
com.sample.Utility;
查看更多
冷血范
3楼-- · 2020-07-17 08:01

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).

查看更多
smile是对你的礼貌
4楼-- · 2020-07-17 08:10

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).

查看更多
欢心
5楼-- · 2020-07-17 08:11

Programs are organized as sets of packages. Each package has its own set of names for types, which helps to prevent name conflicts.

For small programs and casual development, a package can be unnamed or have a simple name, but if code is to be widely distributed, unique package names should be chosen using qualified names. This can prevent the conflicts that would otherwise occur if two development groups happened to pick the same package name and these packages were later to be used in a single program.

Others by language specification

查看更多
萌系小妹纸
6楼-- · 2020-07-17 08:18

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 and java.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 writing java.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".

  • Classes that have package-private methods and/or fields will not be accessible across package boundaries, which can be desirable in certain cases.

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:

MP3
FLAC
Vorbis
AAC

...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.

name.makoto.format
  - MP3
  - FLAC
  - Vorbis
  - AAC

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?

name.makoto
    - reader
        MediaReader
    - format
        MP3
        FLAC
        Vorbis
        AAC

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.

name.makoto
    - reader
        MediaReader
    - format
        MP3
        FLAC
        Vorbis
        AAC
        - impl
            MP3Impl
            FLACImpl
            VorbisImpl
            AACImpl

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.

查看更多
狗以群分
7楼-- · 2020-07-17 08:21

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).

查看更多
登录 后发表回答