Why shouldn't we use the (default)src package?

2020-02-14 08:28发布

I recently started using Eclipse IDE and have read at a number of places that one shouldn't use the default(src) package and create new packages.
I just wanted to know the reason behind this.

6条回答
看我几分像从前
2楼-- · 2020-02-14 08:59

Originally, it was intended as a means to ensure there were no clashes between different pieces of Java code.

Because Java was meant to be run anywhere, and over the net (meaning it might pick up bits from Sun, IBM or even Joe Bloggs and the Dodgy Software Company Pty Ltd), the fact that I owned paxdiablo.com (I don't actually but let's pretend I do for the sake of this answer) meant that it would be safe to call all my code com.paxdiablo.blah.blah.blah and that wouldn't interfere with anyone else, unless they were mentally deficient in some way and used my namespace :-)

From chapter 7, "Packages", of the Java Language Spec:

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

I actually usually start by using the default package and only move it into a real package (something fairly easy to do with the Eclipse IDE) if it survives long enough to be released to the wild.

查看更多
Bombasti
3楼-- · 2020-02-14 09:05

The main reasons I can think of are:

  1. It keeps things organised, which will help you (and others!) know where to look for classes/functionality.
  2. You can define classes with the same name if they are in different packages.
  3. Classes/etc in the default package cannot be imported into named packages. This means that in order to use your classes, other people will have to put all their classes in the default package too. This exacerbates the problems which reasons 1 & 2 solve.
查看更多
手持菜刀,她持情操
4楼-- · 2020-02-14 09:10

Using the default package may create namespace collisions. Imagine you're creating a library which contains a MyClass class. Someone uses your library in his project and also has a MyClass class in his default package. What should the compiler do? Package in Java is actually a namespace which fully identifies your project. So it's important to not use the default package in the real world projects.

查看更多
仙女界的扛把子
5楼-- · 2020-02-14 09:20

By declaring a package you define your own namespace (for classes). This way if you have two identical classes using a different package name (namespace) will differentiate between which one you want to use.

查看更多
beautiful°
6楼-- · 2020-02-14 09:21

From a java point of view, there are two general dev/deploy lifecycles you can folllow, either using ant to build and deploy, or the maven lifecycle. Both of these lifecycles look for source code and resources in local directories, and in the case of maven, in defined repositories, either locally or on the net.

The point is, when you set up a project, for development and eventually deployment, you want to build a project structure that is portable, and not dependent on the IDE, ie. your project can be built and deployed using either of your build environments. If you use a heavy dependence on the Eclipse framework for providing class variables, compile paths, etc.. you may run into the problem that your project will only build and deploy using that configurationj, and it may not be portable to another developers environment, so to speak.

查看更多
Bombasti
7楼-- · 2020-02-14 09:22

Java uses the package as a way to differentiate between classes. By using packages, you can have an org.example.Something class and an org.example.extended.Something class and be able to differentiate between them even though they are both named Something. Since their packages are different, you can use them both in the same project.

查看更多
登录 后发表回答