• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Classpath, packages, huh?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Die Fledermaus does not fear such a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic