Java Packages

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.

This is how a package hierarchy looks like:

↳project-folder
 ↳src
  ↳com
   ↳example
    ↳Student.java

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

Example

//this is declaration of the package name
package com.tutorialsbuddy.model;

public class MyExample {

    public static void main(String[] args) {
        System.out.println("Java Tutorial");
    }

}
 

Import 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.

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

How to import a single Class/Interface?

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

import package.name.className;
 
Example

import java.util.List;
 

How to import entire Classes/Interfaces of a package?

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

Syntax

import package.name.*;
 
Example

import java.util.*;
 

Types of Packages

There are two types of packages in Java:

  1. Java Built-in packages — These are packages which are included in the Java development libraries. These packages contains pre-written Java classes and interfaces.
  2. 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);
        }
    
    }
     
  3. User-defined packages — These are your own packages which you can create to group your classes and interfaces.
  4. 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:

    Example
    
        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("[email protected]");
    
            System.out.println("The student name is " + student.getName() + 
            " and the email is " +student.getEmail());
        }
    }
     
    Output
    The student name is Rose and the email is [email protected]