|Navigate Language Fundamentals topic:|
Usually a Java application is built by many developers and it is common that third party modules/classes are integrated. The end product can easily contain hundreds of classes. Class name collision is likely to happen. To avoid this a Java class can be put in a "name space". This "name space" in Java is called the package.
The Java package needs to be unique across Vendors to avoid name
collisions. For that reason Vendors usually use their domain name
in reverse order. That is guaranteed to be unique. For example a
company called 'Your Company Inc.', would use a package name
something like this:
To put a class in a package, the
package keyword is
used at the top of each class file. For Example,
When we want to reference a Java class that is defined outside of the current package name space, we have to specify which package name space that class is in. So we could reference that class with something like com.yourcompany.yourapplication.yourmodule.YourClass . To avoid having to type in the package name each time when we want to reference an outside class, we can declare which package the class belongs to by using the import Java keyword at the top of the file. For Example,
Then we can refer to that class by just using the class name YourClass .
In rare cases it can happen that you need to reference two classes having the same name in different packages. In those cases, you can not use the import keyword for both classes. One of them needs to be referenced by typing in the whole package name. For Example,
importcom.yourcompany.yourapplication.youmodule.SameClassName; ... SameClassName yourObjectRef = new SameClassName(); com.hercompany.herapplication.hermodule.SameClassName herObjectRef = new com.hercompany.herapplication.hermodule.SameClassName();
The Java package has one more interesting characteristic; the package name corresponds where the actual file is stored on the file system. And that is actually how the compiler and the class loader find the Java files on the file system. For example, the class com.yourcompany.yourapplication.yourmodule.YourClass, is stored on the file system in the corresponding directory : com/yourcompany/yourapplication/yourmodule/YourClass. Because of this, package names should be lowercase, since in some operating systems the directory names are not case sensitive.
It is possible to import an entire package, using an asterisk:
While it may seem convenient, it may cause problems if you make
a typographical error. For example, if you use the above import to
use JFrame, but then type
JFraim frame=new JFraim();,
the Java compiler will report an error similar to "Cannot find
symbol: JFraim". Even though it seems as if it was imported, the
compiler is giving the error report at the first mention of JFraim,
which is half-way through your code, instead of the point where you
imported JFrame along with everything else in javax.swing.
If you change this to
the error will be at the import instead of within your code.
Furthermore, if you
import javax.swing.*; and
import java.util.*;, and javax.swing.Queue is later
added in a future version of Java, your code that uses Queue
(java.util) will fail to compile. This particular example is fairly
unlikely, but if you are working with non-Sun libraries, it may be
more likely to happen.
If you are importing library packages or classes that reside in
.jar file, you must ensure that the file is in the
current classpath (both at compile- and execution-time). Apart from
this requirement, importing these packages and classes is the same
as if they were in their full, expanded, directory structure.
HashMapclass is in the
Customerclass with different name space (in different package).
importkeyword only for one of the class. For the other we need to use the fully qualified name.