codingstreets
Search
Close this search box.

Introduction to Java Package

woman in gray shirt sitting on bed
Photo by Andrew Neel on Pexels.com

Java Package – This article is about Java Package, which describes the Java Package, like what the package is, various types of packages, and its implementation of it.

Before moving ahead, let’s learn a bit about Modifier in Java.

Table of Contents

Package

A Package in Java is used as a folder to group similar things like files, classes, etc. In other words, a Package is the collection of classes and interfaces based on their functionality.

For example, we create a package (folder) named subjects, and inside it, store multiple files with different names like Eng, Sci, Math, etc. 

Types of Package

There are two types of packages in java:

  1. Built-in defined package
  2. User-defined package

Built-in defined package – A built-in defined package is already defined in Java.

User-defined package – A user-defined package is defined by the user or not previously stored in Java.

How to import a Java Package?

  1. import java.lang.ClassName → import Class from java.lang
  2. import java.lang.* → * means import every class from java.lang
  3. A = new java.util.String(“String”) → Use without importing

E.g.; java.util.Scanner;

java.util is a built-in defined package in Java that imports the Scanner class from the Java library to take input from the user.

Importing the built-in defined package

Example: Import the Scanner class from java.util package to take input from the user.

				
					import java.util.Scanner;

public class User {
    public static void main(String[] args) {
        Scanner Number = new Scanner(System.in);
        
        System.out.println("Enter your number below:");
        int a = Number.nextInt();
        Number.close();

        System.out.println("User entered number is: " + a);
    }
}

				
			

In the above example, we import the Scanner class from the Java library (java.util). 

Example: import everything from java.util package to take input from the user.

				
					import java.util.*;

public class User {
    public static void main(String[] args) {
        Scanner Number = new Scanner(System.in);
        
        System.out.println("Enter your number below:");
        int a = Number.nextInt();
        Number.close();

        System.out.println("User entered number is: "+ a);
    }
}

				
			

But in this example, we did not define a particular class to import from the Java library; instead, we used a *, which means it will import every class from java.util.

Example: Direct the path direction of the Scanner class to the java.util package.

				
					public class User {
    public static void main(String[] args) {
        java.util.Scanner Number = new java.util.Scanner(System.in);
        
        System.out.println("Enter your number below:");
        int a = Number.nextInt();
        Number.close();

        System.out.println("User entered number is: "+ a);
    }
}

				
			

In the above example, we directly mentioned the path to import the Scanner class from the Java library by writing the code java.util.Scanner Number = new java.util.Scanner();

Create Java Source File and Compiled & read with Java Interpreter

Create a Java source file.

Create a .class file of Java source file.

Syntax:

				
					javac your file name
				
			

In the above case:

				
					javac User.java 
				
			

Here is the image:

A java source file has been compiled with javac and converted into a bytecode file i.e., .class extension.

Write the following code to execute the bytecode file with the Java interpreter

Syntax:

				
					java your file name
				
			

Note: Do not include .java extension in the file name.

After running the above code in the terminal, you will see the output of your Java file.

Create a package and organize a .class file.

Syntax:

				
					javac -d . your file name
				
			

In the above case:

				
					javac -d . User.java
				
			

Here is the image:


Note: We must mention the package name in the file to create the package. 

Here the first .class file was created by the above code and organized the same in the package name oneforall and the other .class file is the one we created earlier.

What if we have more than one file to create .class files and organize them into a package?

To do this:

Syntax:

				
					javac -d . *.java
				
			

Here is the image:

In the above code, sign * means the Java interpreter selects all files to create an individual .class file for each defined file and organizes them into a package. 

Implementing and applying Packages

To create a package, you have to use the package keyword. The package statement must be the first statement in your program. It must have a unique name.

Syntax:

				
					package package_name
				
			

Example: Create a user-defined package and compile the Java source file.

				
					package mypackage;
public class myfile
{
	public static void main(String[] args)
	{
		System.out.println("Hello I am a learner.");
	}
}

				
			

To Compile in the command prompt:

Syntax: 

				
					javac -d directory javafilename  
				
			

To compile the above program, we have to write the following:

				
					javac -d . myfile.java
				
			

The above command forces the compiler to create a package. The “.” operator represents the current working directory. You can use any directory name.

To Run in the command prompt:

Syntax:

				
					java packagename.classname
				
			

To run the above program, we have to write the following:

				
					java mypackage.myfile 
				
			

Output: Hello I am a learner.

When you execute the code, it creates a package mypackage. When you open the Java package mypackage inside that, you will find myfile.class file.

Importing the user-defined defined package

Example: Import package from the package name.

				
					//save the file with name myfile.java  

package mypackage;
public class myfile 
{
    public void message() 
    {
        System.out.println("Hello I'm from another Package.");
    }   
}   

				
			
				
					//save the file with name newfile.java  

import mypackage.*;                  // * implies all classes of package mypackage

public class newfile 
{
    public static void main(String[] args) 
    {
        myfile object = new myfile();
        object.message();
    }
}

				
			

Output: “Hello I’m from another Package.

Example: Import package from packagename.classname.

				
					//save the file with name myfile.java  

package mypackage;
public class myfile 
{
    public void message() 
    {
        System.out.println("Hello I'm from another Package.");
    }   
}   

				
			
				
					//save the file with name newfile.java  

import mypackage.myfile;          //only import the class myfile from the package.

public class newfile 
{
    public static void main(String[] args) 
    {
        myfile object = new myfile();
        object.message();
    }
}

				
			

Output: “Hello I’m from another Package.

Example: Import package by using a fully qualified name.

				
					//save the file with name myfile.java  

package mypackage;
public class myfile 
{
    public void message() 
    {
        System.out.println("Hello I'm from another Package.");
    }   
}   

				
			
				
					//save the file with name newfile.java  

public class newfile 
{
    public static void main(String[] args) 
    {
        //packagename.classname = packagename.classname 
        mypackage.myfile object = new mypackage.myfile();
        object.message();
    }
}

				
			

Output: “Hello I’m from another Package.

Conclusion

This article was about Java Package, which described types of Package, Implementation of built-in defined Package and User-defined Packages as well as created Java source file and compiled it with Java compiler and at the end, read the bytecode file with Java Interpreter.

Please comment below if you need help with something in the above-discussed topic and have further questions.

Connect on:

Recent Articles