Packages in Java How To Create and Use Packages in Java
Packages in Java How To Create and Use Packages in Java
edureka.co/blog/packages-in-java
Re-usability: The classes contained in the packages of another program can be easily
reused
Name Conflicts: Packages help us to uniquely identify a class, for example, we can
have company.sales.Employee and company.marketing.Employee classes
Controlled Access: Offers access protection such as protected classes, default classes
and private class
Data Encapsulation: They provide a way to hide classes, preventing other programs
from accessing classes that are meant for internal use only
Maintainance: With packages, you can organize your project better and easily locate
related classes
It’s a good practice to use packages while coding in Java. As a programmer, you can easily figure out
the classes, interfaces, enumerations, and annotations that are related. We have two types of
packages in java.
1. Built-in Packages
1/10
2. User Defined Packages
Built-in Packages
Built-in packages or predefined packages are those that come along as a part of JDK (Java
Development Kit) to simplify the task of Java programmer. They consist of a huge number of
predefined classes and interfaces that are a part of Java API’s. Some of the commonly used built-in
packages are java.lang, java.io, java.util, java.applet, etc. Here’s a simple program using a built-in
package.
1 package Edureka;
2 import java.util.ArrayList;
3 class BuiltInPackage {
6 myList.add( 3 );
7 myList.add( 2 );
8 myList.add( 1 );
}
10
}
11
12
13
14
15
16
Output:
2/10
The ArrayList class belongs to java.util package. To use it, we have to import the package using the
import statement. The first line of the code import java.util.ArrayList imports the java.util package and
uses ArrayList class which is present in the sub package util.
1 package MyPackage;
The package statement simply specifies to which package the classes defined belongs to..
Note: If you omit the package statement, the class names are put into the default package, which has
no name. Though the default package is fine for short programs, it is inadequate for real
applications.
3/10
To create a class inside a package, you should declare the package name as the first statement of
your program. Then include the class as part of the package. But, remember that, a class can have
only one package declaration. Here’s a simple program to understand the concept.
1 package MyPackage;
5 num1 = n;
6 num2 = m;
7 }
}
11
else {
12
System.out.println( "Maximum value of two numbers is " + num2);
13
}
14
}
15
public static void main(String args[]) {
16
Compare current[] = new Compare[ 3 ];
17
current[ 1 ] = new Compare( 5 , 10 );
18
current[ 2 ] = new Compare( 123 , 120 );
19
for ( int i= 1 ; i < 3 ; i++)
20
{
21
current[i].getmax();
22
}
23
}
24
}
25
4/10
26
27
28
29
30
31
Output:
As you can see, I have declared a package named MyPackage and created a class Compare inside
that package. Java uses file system directories to store packages. So, this program would be saved
in a file as Compare.java and will be stored in the directory named MyPackage. When the file gets
compiled, Java will create a .class file and store it in the same directory. Remember that name of the
package must be same as the directory under which this file is saved.
You might be wondering how to use this Compare class from a class in another package?
5/10
1 package Edureka;
2 import MyPackage.Compare;
5 int n= 10 , m= 10 ;
7 if (n != m) {
8 current.getmax();
9 }
10 else {
15
Output:
I have first declared the package Edureka, then imported the class Compare from the package
MyPackage. So, the order when we are creating a class inside a package while importing another
package is,
Package Declaration
Package Import
Well, if you do not want to use the import statement, there is another alternative to access a class file
of the package from another package. You can just use fully qualified name while importing a class.
6/10
Here’s an example to understand the concept. I am going to use the same package that I have
declared earlier in the blog, MyPackage.
1 package Edureka;
4 int n= 10 , m= 11 ;
7 if (n != m) {
8 current.getmax();
}
9
else {
10
System.out.println( "Both the values are same" );
11
}
12
}
13
}
14
Output:
In the Demo class, instead of importing the package, I have used the fully qualified name such as
MyPackage.Compare to create the object of it. Since we are talking about importing packages, you
might as well check out the concept of static import in Java.
7/10
1 package MyPackage;
10 }
11 }
Output:
Though using static import involves less coding, overusing it might make program unreadable and
unmaintainable. Now let’s move on to the next topic, access control in packages.
The table below gives a real picture of which type access is possible and which is not when using
packages in Java:
8/10
Same Class Yes Yes Yes Yes
This way, Java packages provide access control to the classes. Well, this wraps up the concept of
packages in Java. Here are some points that you should keep in mind when using packages in Java.
Points to Remember
Every class is part of some package. If you omit the package statement, the class names are
put into the default package
A class can have only one package statement but it can have more than one import package
statements
The name of the package must be the same as the directory under which the file is saved
When importing another package, package declaration must be the first statement, followed by
package import
Well, this brings us to the end of this ‘Packages in Java’ article. We learned what a package is and
why we should use them. There is no doubt that Java package is one of the most important parts for
efficient java programmers. It not only upgrades the programmer’s coding style but also reduces a lot
of additional work.
If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java
Concepts.
9/10
Java Certification Training Course
Weekday / Weekend Batches
If you found this article on “Packages in Java”, check out the Java Course Online by Edureka, a
trusted online learning company with a network of more than 250,000 satisfied learners spread
across the globe. We are here to help you with every step on your journey, for becoming a besides
this java interview questions, we come up with a curriculum which is designed for students and
professionals who want to be a Java Developer. Join our Java Training in Cambridge today.
10/10