Introduction
Definition
Benefit of package
- A Java package is a mechanism for collecting Java classes into a folder (package). And the folder is compressed.
- It is used to provide a large number of APIs in a hierarchical form and makes your program lightweight. Because if all the APIs were provided in a single package then when you import that package into your program, your program would be heavyweight.
- And according to your needs, you make your own package and store the class file within your package.
Thus we can say that
- Every class is part of a package.
- All classes in a file are part of the same package.
- You can specify the package using a package declaration:
package name as the first (non-comment) line in the file. - Multiple files can specify the same package name.
- You can access public classes in another (named) package using:
package-name.class-name - You can access the public fields and methods of such classes using:
package-name.class-name.field-or-method-name - You can avoid having to include the package-name using:
or
- import package-name.*;
at the beginning of the file (after the package declaration). The former imports all of the classes into the package, and the second imports just the named class.- import package-name.class-name;
- You must still use: Class-name to access the classes in the packages, and
class-name.field-or-method-name to access the fields and methods of the class; the only thing you can leave off is the package name.
Some core packages of Java are:
| Package Name | Description |
| java.lang | Basic language functionality and fundamental types |
| java.util | Collection data structure classes |
| java.io | File operations |
| java.math | Multiprecision arithmetics |
| java.nio | The New I/O framework for Java |
| java.net | Networking operations, sockets, DNS lookups |
| java.security | Key generation, encryption and decryption |
| java.sql | Java Database Connectivity to access databases |
| java.awt | The basic hierarchy of packages for native GUI components |
| javax.swing | Hierarchy of packages for platform-independent rich GUI |
| java.applet | Classes for creating an applet |
Create It's Own package
Defining a Package
Syntax
package packageName;
To create a hierarchy of packages, separate each package name from the one above it with a period. The general form of a multilevel package statement:
How to create a hierarchy of packages
Syntax
package pkg1[.pkg2[.pkg3]];
Example
In this example, we create a folder named Package and save this Java file with the name OwnPackage.java. The package name and folder name should be the same:
OwnPackage.java
- package Package;
- public class OwnPackage
- {
- public static void main(String args[])
- {
- System.out.println("this Example of creating own package");
- System.out.println("Myself Abhishek dubey");
- System.out.println("you can put n number of class in your package");
- System.out.println("this is enough to under stand package");
- }
- }
OUTPUT
After running this code the output will be as follows:
How to Use package
import pkg1[.pkg2].(classname.*);
(*) means including all classes under that package.
For example
Example
In this example, we are going to import the util package to use the class, as in:
- import java.lang.*;
- import java.util.Date;
- public class PackageDemo
- {
- public static void main(String arg[])
- {
- System.out.println(new Date());
- System.out.println("My self Abhishek dubey");
- System.out.println("Its example How we import java package");
- }
- }
Output

Sandeep SharmaPosted May 3, 2013, 12:09 AM
nice article...
Sam HobbsPosted May 3, 2012, 1:22 PM
C++ has namespaces that are very much like C# namespaces. Header files in C and C++ provide a similar purpose but they are very different in the way they work. C and C++ header files are not equivalent to anything in C# since the designers of C# do not like header files.