Java Programming/Packages
From Wikibooks, the open-content textbooks collection
| Navigate Language Fundamentals topic: |
Contents |
[edit] Java Package / Name Space
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: com.yourcompany.yourapplicationname.yourmodule.YourClass.
To put a class in a package, the package keyword is used at the top of each class file. For Example,
package com.yourcompany.yourapplication.yourmodule;
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,
import com.yourcompany.yourapplication.yourmodule.YourClass;
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,
packagecom.mycompany.myapplication.mymodule; ...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.
[edit] Wildcard imports
It is possible to import an entire package, using an asterisk:
import javax.swing.*;
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 import javax.swing.JFraim; 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.
[edit] Importing packages from .jar files
If you are importing library packages or classes that reside in a .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.
To compile and run a class from a project's top directory (that contains the two directories /source and /libraries) you could use the following command:
javac -classpath libraries/lib.jar source/MainClass.java
And then to run it, similarly:
java -classpath libraries/lib.jar source/MainClass
(The above is simplified, and demands that MainClass be in the default package, or a package called 'source', which isn't very desirable.)
[edit] Class Loading / Name Space
- A fully qualified class name
- consist of the package name plus the class name.
- For example, the fully qualified class name of
HashMapisjava.util.HashMap. - Sometime is can happen that two class has the same name, but it can not have on the same package, otherwise it would be the same class.
- It can be said that the two class with the same name is in different name space. In the above example, the
HashMapclass is in thejava.utilname space.
- Let be two
Customerclass with different name space (in different package).com.bluecompany.Customercom.redcompany.Customer
- When we need to use both class in the same program file, we can use the
importkeyword only for one of the class. For the other we need to use the fully qualified name.
- The runtime identity of a class in Java 2
- is defined by the fully qualified class name and its defining class loader. This means that the same class, loaded by two different class loaders, is seen by the Virtual Machine as two completely different types.