PDA

Click to See Complete Forum and Search --> : packages


DNA7433
Apr 9th, 2007, 05:34 PM
Why do you have to specify the package that a class is in if you are also forced to place it in the same directory structure? It seems redundant, and if you move the class to a different directory you also have to change it's package declaration.

Is there a purpose for that design?

CornedBee
Apr 10th, 2007, 03:51 AM
It seems to be a misunderstanding on your part. The directory structure is used to quickly find the code for a class given its full name, not the other way round. If the directory structure was used to find out the package, you'd always have to give javac the base directory to work from. You could run into problems with case-insensitive systems like Windows. And there is no actual requirement for the source code to be in the directory structure - it's just convenient.

DNA7433
Apr 11th, 2007, 04:02 PM
Hmm, that's the first time your answer has made no sense to me. :confused:

The directory structure is used to quickly find the code for a class given its full name, not the other way round.Ok, but why do you specify the package in the file where you define the class?

there is no actual requirement for the source code to be in the directory structure I thought javac spit out errors if your class was not in the same directory structure as it's package declaration says it is?

CornedBee
Apr 12th, 2007, 03:50 AM
No, it doesn't. It's only java that won't be able to find it. If given the -D option, however, javac will automatically put the .class files in their proper directories.

You specify the package name for another reason: the source file should contain the entire information about the class; the file name and location should not affect the meaning of code. That's just good design practice, because the other way is too brittle. Compile the file anywhere else - in fact, simply with a slightly different compiler invocation -, and -- oops, class now has a different FQN and can't be loaded anymore. Sorry.

Supposing this were my directory structure:
/home/wasti
/projects
/scm
/release
/src
/at
/getdesigned
/scm
/dao
FooDao.java
BarDao.java
FoobarDao.java
/music
What package do these three interfaces belong to? Well, depending on the command line, it could be any of these:
/ $ javac ... # package home.wasti.projects.scm.devel.src.at.getdesigned.scm.dao
~ $ javac ... # package projects.scm.devel.src.at.getdesigned.scm.dao
~/projects/scm/devel $ javac ... # package src.at.getdesigned.scm.dao
~/projects/scm/devel/src $ javac ... # package at.getdesigned.scm.dao
~/projects/scm/devel/src/at/getdesigned $ javac ... # package scm.dao
Bad - depending on from where I invoke the compiler, the code has different meanings. The absurdity of this is staggering.

Or think about this scenario: the file is for download on a web page - how do you know which directory to correctly put it in? Say, it's an update for an application - "grab this file, put it in its directory and recompile".