Packages in Java | How to create and use Packages in Java

A package in java is defined as the group of related classes and interfaces into a single unit. For example: Suppose you want to perform database operations, the required classes and interfaces are grouped into a single unit which is java.sql package. Therefore we can say that packages are the encapsulation mechanism to group related classes and interfaces into a single unit. There are two types of packages in Java: built-in packages and user-defined packages.


Advantages of using Packages in Java:

1. Naming Conflicts: There are 2 Date classes available in Java, one inside the SQL package and second inside the util package. So, by using the packages we can able to differentiate java.util.date and java.sql.Date. If a package statement is not there than it is impossible to have 2 date classes. By using a package, we can able to resolve naming conflicts.

2. Modularity and Maintainability: As we know, Packages are the group of related classes and interfaces, therefore, Modularity and the Maintainability of the application are gone to be improved.

3. Security: Suppose you have a class inside a package pack1. Assume this class is not public if it is not public then outside person not able to access this class because the default is only accessible within the same package. The package provides security to our component.



Types of packages in Java.

There are two types of packages in Java:
packages in java


1. Built-in package: The packages which are already defined and part of Java API is known as Built-in packages. Some commonly used built-in packages are given below:
i) java.lang: This package contains language support classes.
ii) java.util: This package contains utility classes such as LinkedList, Dictionary, etc.
iii) java.io: This package contains classes for file read/write operations.
iv) java.net: This package contains classes for networking operations.
v) java.applet: This package contains classes for creating the applets.

2. User-defined package: The packages that are defined by the user is known as a user-defined package.


How to create packages in Java.

Creating a package in Java is not a difficult task. You can create a package in java by using the package keyword. for example:

package MyPackage;
Here package is a keyword and MyPackage is a package name.


How to compile and run Java Package.

Suppose we have a file called HelloWorld.java, and we want to put this file in a package world. for example:

package World;
public class HelloWorld{
	public static void main(String args[]){
		System.out.println("Hello World");
	}
}


Now compile this file as:

javac -d . HelloWorld.java
Here -d specifies the destination where to put the class file and . (dot) specifies the current working directory.

You can run this file as:

java World.HelloWorld
We need to use the fully qualified name to run the class. Here World is a package and HelloWorld is a class inside this package.


How to access the package from another package.

There are three ways to access the package from another package.
1. import package.*;
2. import package.classname;
3. using a fully qualified class name.


1. Using a package name: 

If we use package.* then all the classes and interfaces of this package accessible. We can use the import keyword to access the classes and interfaces of another package to the current package. Let's see the example.


Example of Java package that imports the package-name.*;


//save by class1.java.
package pack1;
public class Class1{
	public void display(){
		System.out.println("Inside class1 method.")
	}
}


//save by Class2.java
package pack2;
import pack1.*;
class Class2{
	public static void main(String args[]){
		Class1 obj = new Class1();
		obj.display();
	}
}

Output:

Inside class1 method.


2. Using the package.classname:

We can use the import package.classname to access the only declared class of this package. Let's see the example.

Example of the package in java using the package.classname


//save by class1.java.
package pack1;
public class Class1{
	public void display(){
		System.out.println("Inside class1 method.")
	}
}


//save by Class2.java
package pack2;
import pack1.Class1;
class Class2{
	public static void main(String args[]){
		Class1 obj = new Class1();
		obj.display();
	}
}

Output:

Inside class1 method.


3. Using a fully qualified class name.

If we use a fully qualified class name then only declared class of this package will accessible and there is no need to import. In this case, we need to use a fully qualified name every time when you are accessing the class or interface.


Example of a package in Java using a fully qualified class name:


//save by class1.java.
package pack1;
public class Class1{
	public void display(){
		System.out.println("Inside class1 method.")
	}
}


//save by Class2.java
package pack2;
class Class2{
	public static void main(String args[]){
                //using fully qualified class name.
		pack1.Class1 obj = new pack1.Class1(); 
		obj.display();
	}
}

Output:

Inside class1 method.



You may also like these posts:

1. Final keyword in java with example.
Packages in Java | How to create and use Packages in Java Packages in Java | How to create and use Packages in Java Reviewed by Prashant Srivastava on December 25, 2019 Rating: 5

No comments:

Powered by Blogger.