Java Tutorial-Java Packages

Java Packages


Java Packages & API


A package in Java is used to group the related classes. We use packages to avoid name conflicts and help to write a better maintainable code. There are two categories in packages namely:

  • Built-in packages
  • User-defined packages


Built-in packages


A Java API itself contains a library of pre-written classes. They are free to use and they are included in Java Development Environment.

We can import packages in two ways namely import packages and import classes. Meaning, we can either import a single class (along with methods and attributes) or a whole package that contain all the classes that belong to the specified package.


Syntax

import package.name.Class; //Import a single class
import package.name.* //Import the whole package

Import a Class


If you find a class, for example Scanner class from a java.util package. This class is used to get the user input as shown in the below example:


Example

public class PackageExample {
       public static void main (String args[]){
              Scanner scannerObj = new Scanner(System.in);
              System.out.print("Enter name : ");
             
              String name = scannerObj.nextLine();
              System.out.println("Username is : "+name);
       }
}

Output

Enter name : SparkDatbox
Username is : SparkDatbox

Example Explained


To use the Scanner class, we create a object scannerObj and we can use the methods from the Scanner class using the object created. To know more about the Scanner class methods, refer to the Scanner class documentation. The nextLine() method in the Scanner class is used to read the complete line.

 

Import a Package


There are many packages to choose from. In the previous example, we used the Scanner class from the java.util package. This package also contain other facilities such as date-time, random number generator and other utility classes.

To import a whole package, we end the package with an asterisk (*). The following example will import all the classes in java.util package:


Example

import java.util.*;

User-defined Packages


To create a own package, we need to understand that Java use a file system directory to store them, just like the folders on computer:


Example


|___root

      |___mypack

           |___MyPackageClass.java


To create a package, we use the package keyword


Example

package mypack;
 
public class MyPackageClass {
       public static void main(String args[]){
              System.out.println("We are in the package mypack!");
       }
}

We can compile the MyPackageClass.java as given below:

C:\Users\Your Name>javac MyPackageClass.java


Then we can compile the whole package as given below:

C:\Users\Your Name>javac -d . MyPackageClass.java


 The -d keyword specifies the destination for where to save the class file. You can specify any directory name or also only dot sign “.”, to keep the package within the same package.

 

To run the MyPackageClass.java file, we use the below command:

C:\Users\Your Name>java mypack.MyPackageClass

 

Then the output will be,


Output

We are in the package mypack