On Wed, 26 Aug 1998 15:54:36 +0200, Maarten van Leunen wrote:
>Howdie,
>
>Anyone know how these darned packages work? Or where a good guide is
>available concerning packages?
>
>I can't seem to find anything about it in the nice Sun Java Tutorial.
Well, it is covered in "The Java Programming Language" by Arnold & Gosling.
It is also in the Java Language Specification (which you can download)
The quick-n-dirty:
A package can be seen as a "family" of classes that are more related
than classes outside the package but not as releated as subclasses.
This means that methods that are private or protected can not be seen
by "family" members but they can see the "non-public" and public methods
and members. (The non-public ones are the ones that do not have
private, protected, or public)
A class is defined as part of a package with the package statement.
Such as:
package ORG.blackdown.util;
This package would then be in a directory called:
ORG/blackdown/util/
Note that the class would then be part of that package. Normally
you would have the source (.java) files in that directory (or equal
directory in a different tree) so that it matches it package location.
(Otherwise javac will not know how to automatically find it)
Packages members can the be accessed either directly via:
ORG.blackdown.util.MyClass
or ORG.blackdown.util.MyClass.staticMember
or, you can import the class and then act on it as if local (but not
gain package access. Import is just a form of #define to get the names
to be shorter) For example:
import ORG.blackdown.util.MyClass;
MyClass.staticMember ...
Warning - personal opinion/flame about the import feature of Java -----
I personally am rather against most import statements as they can
only help to confuse the issue of what you are looking at. Even worse
is import of a whole package - since then you do not even see in the
code all of the possible class names that just got defined... However,
if used very rarely and only when really justified, they can help
reduce the typing needed and not reduce the maintainability of the code.
Sorry about that, but I have had to fix code where the top of the
file had import xxxx.*; import yyyy.*; etc. Basically importing every
package in the whole product - almost 30 lines of these - and then
trying to figure out what the DataTrack class is and which one it used -
since there was a different one in three different packages...
BTW - This was only one person who did this in his souce - and his
code was usually the code we had to fix. But he was a contractor and
did not follow our coding standards.
Michael Sinz -- Director of Research & Development, NextBus Inc.
mailto:[EMAIL PROTECTED] --------- http://www.nextbus.com
My place on the web ---> http://www.users.fast.net/~michael_sinz