File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Classpath, packages, huh? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classpath, packages, huh?" Watch "Classpath, packages, huh?" New topic

Classpath, packages, huh?

Bret Waldow
Ranch Hand

Joined: Aug 04, 2000
Posts: 59
I don't understand about packages and classpaths. In C++, we can put code into namespaces, and that makes a grouping for the code. That I understand.
With Java, somehow packages have to do with file directories.
If I have a physical directory named /code/project with files in it, and subdirectories with more files, like /code/project/drivers and /code/project/filestuff, what package would I use in the java files? Would I use different packages in the java files in the subdirectories? If it's optional, why would I choose one way or the other?
That last question is probably the most important - why would I put my code into packages? When would I bother, and how should I decide?
Thanks in advance for any help.
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 782

If I have a physical directory named /code/project with files in it, and subdirectories with more files, like /code/project/drivers and /code/project/filestuff, what package would I use in the java files?

If you have a file called in /code/project, its package declaration will be . And if is in /code/project/drivers, use .
why would I put my code into packages?

Because if two files from different authors share the same classname, the JVM would not know which one to load.


Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Actually when you name a package for a class to be in, you are telling the compile (and the JVM) look at my classpath for the default directory setting, and then look at the package to find the subdirectories of the classpath for the exact location".
So in your example, if the classpath said "c:/code/project" then the packages would be "package drivers" or "package filestuff".
But if the classpath was "c:/code" then the packages would be "package project.drivers" or "package project.filestuff".
When you "jar" a package (which is much the same as zipping it) you retain the sub-directory alignments.
In addition to Pho's good explaination that packages define namespaces for clarification, there is also the access restrictions that java has. By putting classes in different packages you can control which outside classes are allowed to get at and use the classes. For instance Sun has some very low level type activity in classes that really should not be used by you and I. So they have packaged those with default only access in Sun packages(only other classes in the same package can use them). So the only way that I could use them directly would be to specifically break their jar open and extract the files into my stuff. A serious enough infraction that I would know that I was doing something that I shouldn't unless I REALLY know what I am doing.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

I second that. Packages offer two additional levels of scope that in C I sometimes feel are missing.
An indirect point: the compiler only allows for one public class in each file, which makes it impossible for two public classes to share "module scope." Packages solve that problem.
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
I agree. Here's the link:
subject: Classpath, packages, huh?
It's not a secret anymore!