If you’re using java as programming language, then you’ll work with lot of packages and sub-packages. If you want to program effectively then you should understand what packages are, as from compilers view point packages and its nested packages has nothing to do with each other. So in this article you’ll learn about the packages in java.
What is package?
Java allows you to group classes into a collection, this collection is called as package. Java library have large number of packages like java.lang or java.net etc. These packages are usually hierarchical in nature, so you’ll find lot of sub-packages under java and javax package node.
Why use packages?
It allows you to organize your projects. Also, it helps resolve naming conflicts when different packages have classes with the same names. For example, if you’ve class Bird that has two different implementation but same name, if you’ve placed it into different packages there will be no conflict. So the purpose of the packages is to manage the unique names throughout your program.
How to use packages?
As per Sun Microsystem’s recommendation you can use the domain-name in the reverse order like this (com.company.directory) To create package you’ve to use the keyword “package” followed by the path of your class file (e.g this path could be reverse of your domain name as explained earlier)
package com.xyz.client;
In above package com.xyz.client is the package that will be used by your class file.
Adding class to package
To add the source class file to your package, you’ve to write the fully qualified name of the package name to your source file at the top/beginning of the class. If that name is not provided then the class will refer to the default package name, default package name has no name.
package com.xyz.client
public class myclass{
….//
}
Some points to note:
- A class can use all the classes from its own packages & all other public class from other packages.
- You can access single class from package using import statement it helps compiler to understand which specific class you want, to import all the package you can use the .* notation before semicolon while importing package (For example, import java.awt.evet.*;
- Locating the class in a package is the job of compiler, it uses fully qualified package names to refer to the other packages.
- When you put your class into the package, you’ve to use the qualified name for the class dto search or compile it. If the top level directory is used to compile then virtual machine might have problem finding class even though compiler compiled the source file without problem.
- The directory structure of the package must have close relationship with the class in the package where it resides.
- “import” statement used must have fully classified name. Only the final directory where class file is located is searched.
I hope the information above helped. I you’ve any questions or comments, suggestions then don’t hesitate to post them here. Just post your questions.