Java

How To Use Packages In Java?

As the name implies, a package is a collection of classes, interfaces, and other packages. Java classes and interfaces are organized using packages. In Java, there are two sorts of packages: built-in packages and packages that may be created (also known as user defined package). What packages are, what user-defined packages in Java are, and how to utilize them are all covered in this article.

We may import a package like this when we require user input in Java since there are many built-in packages available:

import java.util.Scanner

Advantages of using a package in Java

The following are the justifications for using packages in Java:

Reusability: When working on a Java project, we often feel as if we are writing the same few pieces of code again. When using packages, you may construct these things in the form of classes within a package, then just import that package and use the class anytime you need to execute the same action.
Better Organization: Once more, in large Java projects with hundreds of classes, it is always necessary to group the similar class types into a meaningful package name. This will help you better organize your project and make it easier to find and use the resources you need when you need them, which increases efficiency.
Name Conflicts: To prevent name collisions, we might specify two classes with the same name in distinct packages.

Types of packages in Java

We have two sorts of packages in Java, as was stated at the outset of this article.
1) User-defined package: This is the name of the package we develop.
2) Built-in package: Built-in packages are predefined packages like java.io.*, java.lang.*, etc.

After discussing built-in packages, let’s move on to user-defined packages using some examples.

Example 1: Java packages

Inside a package called letmecalculate, I’ve built a class called Calculator. Declare the package name in the first sentence of your program to build a class inside of it. There may be only one package declaration per class.
Letmecalculate package contains a calculator.java file.

package letmecalculate;

public class Calculator {
   public int add(int a, int b){
	return a+b;
   }
   public static void main(String args[]){
	Calculator obj = new Calculator();
	System.out.println(obj.add(10, 20));
   }
}

Let’s now see how to apply this package to a different program.

import letmecalculate.Calculator;
public class Demo{
   public static void main(String args[]){
	Calculator obj = new Calculator();
	System.out.println(obj.add(100, 200));
   }
}

I have imported the package letmecalculate in order to utilize the Calculator class. I imported the package known as letmecalculate into the aforementioned application. Only the Calculator class is imported by this. However, if the package letmecalculate has many classes, you may import it in this way to access all of its classes.

import letmecalculate.*;

Example 2: Creating a class inside package while importing another package

As we’ve seen, the initial line in your Java application should be either a package declaration or a package import. Let’s see what should happen when we create a class inside one package while importing another.

//Declaring a package
package anotherpackage;
//importing a package
import letmecalculate.Calculator;
public class Example{
   public static void main(String args[]){
	Calculator obj = new Calculator();
	System.out.println(obj.add(100, 200));
   }
}

Example 3: Using fully qualified name while importing a class

To get around the import statement, utilize fully qualified names. Here is an illustration to help you understand:

Calculator.java

package letmecalculate;
public class Calculator {
   public int add(int a, int b){
	return a+b;
   }
   public static void main(String args[]){
	Calculator obj = new Calculator();
	System.out.println(obj.add(10, 20));
   }
}

Example.java

//Declaring a package
package anotherpackage;
public class Example{
   public static void main(String args[]){
        //Using fully qualified name instead of import
	letmecalculate.Calculator obj = 
		new letmecalculate.Calculator();
	System.out.println(obj.add(100, 200));
   }
}

Instead of importing the package, I created the object of the Example class by using the fully qualified name, such as package name.class name. Read about Java’s static import if you’d like.

Sub packages in Java

Sub packages are packages that are contained inside other packages. For instance, a package within the letmecalculate package would be referred to as a sub package.

Imagine that I have added a new package to letmecalculate with the name of multiply. Therefore, the package declaration for this subpackage should come first when I create a class in it.

package letmecalculate.multiply;

Multiplication.java

package letmecalculate.multiply;
public class Multiplication {
	int product(int a, int b){
		return a*b;
	}
}

Now, to utilize this Multiplication class, I must either import the package using the following syntax:

import letmecalculate.multiply;

alternatively I might use a name that qualifies, like this:

letmecalculate.multiply.Multiplication obj = 
     new letmecalculate.multiply.Multiplication();

Points to remember:

1. Class name conflicts may happen sometimes. Let’s use the example of two packages, abcpackage and xyzpackage, each of which has a class with the name JavaExample.java. Let’s say a class imports both of these packages as follows:

import abcpackage.*;
import xyzpackage.*;

Compilation errors will result from this. You must use the fully qualified name technique I’ve explained above if you want to prevent such issues. For instance

abcpackage.JavaExample obj = new abcpackage.JavaExample();
xyzpackage.JavaExample obj2 = new xyzpackage.JavaExample();

You may bypass the import package statements and the name conflict problem in this manner.

2. Although I previously touched on this above, I’ll address it one more. The package declaration should come first, followed by the package import, if we are creating a class while importing another package. For instance:

package abcpackage;
import xyzpackage.*;

3. A class may have more than one package import statement but only one package declaration. For instance:

package abcpackage; //This should be one
import xyzpackage;
import anotherpackage;
import anything;

4. When dealing with subpackages, it’s important to utilize the wildcard import like package.* carefully. Say, for instance, that we have the package abc and the package foo inside of it. Foo is now a subpackage.

Example1, Example2, and Example3 are the classes within ABC while Demo1, Demo2 are the classes inside FOO.

Consequently, if I import the package ABC using the wildcard syntax:

import abc.*;

The classes of the sub package will not be imported; just the classes Example1, Example2, and Example3 will be imported.

You must import the following to import the classes of the subpackage:

import abc.foo.*;

Demo1 and Demo2 will be imported, but not Example1, Example2, or Example3.

Therefore, two import lines in the form of the following must be used to import every class found in package and subpackage:

import abc.*;
import abc.foo.*;

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button