Packages in Java help organize code by providing a layer of encapsulation and control. Packages group related classes and interfaces together, and allow classes to be made private to a package. To define a package, use the package statement at the top of a file with the package name. Multiple files can belong to the same package. Packages are hierarchical, with names separated by periods. Directories mirror package hierarchies.
1. PACKAGES
It’s a group of related classes and interfaces; it helps to organize your code and provide
another layer of encapsulation and also helps to provide another level of control over the java
code.
Packages serve two purposes
It provides mechanism by which related pieces of a program can be organized as a
unit.
Classes defined within a package must be accessed through their package name
Where packages provide a way to name collection of classes.
Second, a package participates in java’s access control mechanism.
Classes defined within package can be made private to that package & not
Accessible outside code.
When a class is defined within package, the name of that package is attached to the class
which will further helpful for name collision
Defining a Package:
To create a Package need to use the Package Statement located at the top of the source file.
And a class declared within that file will belong to specified package.
Syntax
package packageName;
Eg: package pkg;
Here name of the package is pkg
More than one file can include same package statement
You can create hierarchy of Packages and they are separated by period
Eg: package pack1.pack2.pack3.........packN;
One can easily Create directories
Eg: package alpha.beta.gamma;
Stored in ..../alpha/beta/gamma.....
2. Example Code:
package myPackage;
public class DefiningPackage {
public static void main(String[] args) {
System.out.println("This class belongs to myPackage.");
}
}
Output: This class belongs to myPackage
Example2:
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Output: Welcome to package
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
3. Simple Code for Java Package
To Execute the Bellow Code need to go for File then New Project Provide the
Program Name the Go For SRC (Source Code) Then Main
Next Right Click on the Main Next Click on New Then Select Package Shown Bellow
Then one more window will pop up then Need to Provide the Name for the Package you want
to create for Example “mypack” is the name of the Package
4. Now mypack Pakage is Created Shown bellow
Next if you want to create class under your package then you have to click on the main option
provided bellow of your package created Next you have to right click on main option
5. Next you have to create a class by providing name of the class you want to create
Now you can see that you have created your own Package named as mypack.A
6. 1. //save by A.java
2. package mypack;
3. public class A{
4. public void msg(){System.out.println("Hello");}
5. }
6. save by A.java
1. //save by B.java
2. package mypack;
save by B.java
7. 3. import pack.*;
4.
5. class B{
6. public static void main(String args[]){
7. A obj = new A();
8. obj.msg();
9. }
}
Output: Hello When you Run A.java