Why does Java want package names defined? [closed]

2019-07-24 05:30发布

Why can't Java figure it out based on the folder structure?

It seems that the mapping to packages is already specified by the root source folder plus the path to that particular file.

It is completely coupled, and doing a refactor without an IDE is absolutely tedious - although updating the references to that file would be anyway, but it could at least partially be figured out by the compiler rather than specifying the package at the file level.

5条回答
疯言疯语
2楼-- · 2019-07-24 05:54

The main reason for having package is to avoid Name collisions..

So,if you want the compiler to figure out the ArrayList class automatically it won't be able to do it cause you can have your own class named ArrayList.How would compiler know which class to use.So,you need have a package name defined.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-24 06:10

This coupling of directory structure and package name declaration is not a requirement of the Java language. It is imposed by compiler implementations. As explained in Chapter 7 of the Java Language Specification, compilation units do not need to be stored in a file system at all; they can just as easily be in a data base. Also, the language allows package names to contain characters that may be illegal in directory names in the underlying file system.

From the JLS:

As an extremely simple example of storing packages in a file system, all the packages and source and binary code in a project might be stored in a single directory and its subdirectories. Each immediate subdirectory of this directory would represent a top level package, that is, one whose fully qualified name consists of a single simple name. Each further level of subdirectory would represent a subpackage of the package represented by the containing directory, and so on.

Most of our compilers evidently use a variation of this "extremely simple" approach, whereby source and binary code are stored in parallel hierarchies. But nothing about the language requires this.

查看更多
叼着烟拽天下
4楼-- · 2019-07-24 06:11

There is no tight coupling required by the Java Language Specification between a directory/folder structure and the name of a package. Here is a quote from the Java Online Tutorial:

Many implementations of the Java platform rely on hierarchical file systems to manage source and class files, although The Java Language Specification does not require this.

All the IDE's that I know of do manage packages according to a directory structure, but if you really wanted to develop without an IDE and according to a folder structure of your own choosing, it is still possible to do so.

查看更多
看我几分像从前
5楼-- · 2019-07-24 06:13

Folder structures and packages are different although related. For example a src folder called 'src', which holds the entire packages hierarchy may need to be part of the packages. This is especially from the perspective of IDEs like eclipse, where you can create source folders with any heir achy but not related to your package structures.

Another useful info from JLS as a rule that compilers may require:

Packages that are stored in a file system may have certain constraints on the organization of their compilation units to allow a simple implementation to find classes easily.

查看更多
Fickle 薄情
6楼-- · 2019-07-24 06:18

From the JLS 7.2

Each host system determines how packages and compilation units are created and stored.

Each host system also determines which compilation units are observable (§7.3) in a particular compilation. The observability of compilation units in turn determines which packages are observable, and which packages are in scope.

In simple implementations of the Java SE platform, packages and compilation units may be stored in a local file system. Other implementations may store them using a distributed file system or some form of database.

If a host system stores packages and compilation units in a database, then the database must not impose the optional restrictions (§7.6) on compilation units permissible in file-based implementations.

As an extremely simple example of storing packages in a file system, all the packages and source and binary code in a project might be stored in a single directory and its subdirectories. Each immediate subdirectory of this directory would represent a top level package, that is, one whose fully qualified name consists of a single simple name. Each further level of subdirectory would represent a subpackage of the package represented by the containing directory, and so on.

Also read about Compilation Units , closely related to your question.

Note: When you compiling from the command line, by default each class will be put in the same location as the corresponding source file, but if you use the "-d" option the compiler will build the appropriate output directory.

查看更多
登录 后发表回答