File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath, packages, huh?

 
Bret Waldow
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
regards,
Bret
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bret,
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 A.java in /code/project, its package declaration will be . And if B.java 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.
Pho
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic