A package is a group of related Java classes.
They all share the same first name -- e.g., all classes in a package named "com.stinky.util" are named "com.stinky.util.Sorter", "com.stinky.util.FileInfo", and so on.
Classes in the same package can access each other's protected members.
Yeah, it's kind of like friends in C++ -- you know, "only friends can access your private parts." In Java, classes can access the "protected" and "default" member functions and instance data of other classes in the same package (as well as the "public" members, naturally).
One that doesn't have any of the following keywords: public, protected, or private.
You just call it by its full name. For instance,
com.stinky.util.Sorter mysorter = new com.stinky.util.Sorter();
com.stinky.util.Sorter mysorter = new com.stinky.util.Sorter();
Yeah, especially because Java is a strongly typed language.
Seriously, that's what the "import" keyword is for. If you say "import com.stinky.util.Sorter", then every time you type "Sorter", the compiler will understand that you mean com.stinky.util.Sorter. So you can do: import com.stinky.util.Sorter;<BR>
class Foo {<BR>
Sorter mysorter = new Sorter();<BR>
// etc...<BR>
}<BR>
import com.stinky.util.Sorter;<BR>
class Foo {<BR>
Sorter mysorter = new Sorter();<BR>
// etc...<BR>
}<BR>
Yes, the line "import com.stinky.util.*;" will import the names of all classes in the com.stinky.util package. (However, it does not work recursively, so it won't import the com.stinky.util.net.WackySocket class.)
Not really. #include inserts the text of an entire header file and compiles it as if it were part of the original file. import just tells the compiler that when it sees "Sorter", it should replace it with "com.stinky.util.Sorter".
Three things. One: put a line like "package com.stinky.util;" in the very top of your source file. Two: put the source file in a directory named util, in a directory named stinky, in a directory named com.
The third thing is, make sure the top-level package directory ("com" in my example) is inside a directory that's in your classpath.
You don't want to know.
It's a list of directories, stored in an environment variable named CLASSPATH, that Java searches every time it's looking for a source or class file. The trick is, the directories in the classpath don't actually contain the files. They contain the directories that contain the directories that contain the files.
To further confuse matters, the classpath may also contain the names of archive files, specifically ZIP or JAR files. In that case, Java pretends that these archives are actually directories -- directories that contain the files and subdirectories in the archive files.
Just make sure "." (dot) is in your classpath and follow The Rules (see main article).
Be careful out there.
Alex.
Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.
Your name/nickname
Your email
Subject
(Maximum characters: 1200). You have characters left.