Packages in Java

A package in Java is used to group related Java classes and interfaces. In simple terms, a package is a folder in a filesystem directory. It helps to separate one class from another and helps to prevent file conflicts by providing different namespace for every file it contains.

Package name must always be declared as the first line of the code in Java. For example:

package com.tutorialsbuddy.model; //this is package name declaration
public class MyExample { public static void main(String[] args) { System.out.println("Java Tutorial"); } }


Importing Packages

Classes and interfaces in Java are usually divided by packages. Classes and interfaces which are in different packages needs to be imported before using them in the code. You can either import a single class or interface or an entire package that may consist multiple classes and interfaces.

Classes which are within the same package do not need to be imported because they are visible to each other.

Importing a single Class/Interface in Java

A single Class/Interface can be imported using the import keyword followed by the package name and the class name or the interface name.

Syntax for importing a single class or interface in Java:

import package.name.className;

Here is an example of importing List interface from the java.util package :

import java.util.List;


Importing Everything from a package in Java

An entire package can be imported using the import keyword followed by the package name and an asterisk * at the end.

Syntax for importing everything from a package:

import package.name.*;

Here is an example of importing everything from the java.util package:

import java.util.*;


Types of Packages in Java

There are two types of packages in Java:

  • Java Built-in packages : These are packages which are included in the Java development libraries. These packages contains pre-written Java classes and interfaces. Here is an example of importing Java build-in List and ArrayList classes from the java.util package:
package example.test;

import java.util.List;
import java.util.ArrayList;

public class MyExample {

   public static void main(String[] args) {
      List list = new ArrayList<>();
      list.add("Nokia");
      list.add("Samsung");
      list.add("Iphone");
      System.out.println(list);
    }
}
  • User-defined packages : These are your own packages which you can create to group your classes and interfaces. Here, the first line of the code is an example of a user-defined package:
package com.tutorialsbuddy.student;

public class Student {
    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}


Here is an example of importing the above Student class in another Java class of a different package:

package com.tutorialsbuddy.school;

//importing Student class from another package
import com.tutorialsbuddy.student.Student;

public class School {

    public static void main(String args []) {
        Student student = new Student();
        student.setName("Rose");
        student.setEmail("rose@tutorialsbuddy.com");

        System.out.println("The student name is " + student.getName() +
        " and the email is " +student.getEmail());
    }
}

The output of the above code is as follows:

The student name is Rose and the email is rose@tutorialsbuddy.com