Packages in Java -Types Sub Packages and Examples

Packages in Java is one of the important concept. All Java classes and interfaces are arranged in package structure.

What is package in java

Packages is a namespace which contains related classes and Interfaces.

The methods and field of class define inside the package can be accessible in package outside package and in child classes based on access specifier.  

Packages are stored in a hierarchical manner and explicitly imported into a new class definition.

Types of packages in java

Java packages are classified into two types

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

1 Java API(Application Programming Interface) packages or Predefined packages or Built-in packages

These packages are defined by the system. Some example of system defined packages are

  1. java.lang: It contains fundamental classes like System, String Math, Number etc. This is the default package in java.
  2. java.util: This package contains collections framework, legacy collection classes, event model and other features.
  3. java.io: Provides system input output with stream classes and files
  4. java.awt: Contain classes for creating user interface graphic and Image classes.
  5. java.net: Contains classes related to networking and sockets
    The Java 1.3 contains nearly 60 predefined packages

Importing packages in Java with Example

import java.util.Scanner; -To import Scanner class

import java.util.*; To import all classes in util package

Here we import is used to import java.util.Scanner;

2 User defined packages in java

These packages are defined by the user.

How to create package in java

Defining as packages: To define a package, place “package” command as the first statement in the source file.

The syntax of packages creation :-

  • The class that is defined in the package must be public. So that is can be accessible by any another of them.
  • Java uses the file system file directories to store packages. We save the program with number.java and compile the package is as javac_d/number.java. Due to this compilation Mypackage directory is automatically created and .class file is stores in that directory.

Once the package creation has completed. Then the package information is including with the help of “import” keyword.  import keyword links the package with our program.

Java Package Example

It is place before the class definition:

Java Package
Java Package Example
  • In this program, package is within x folder and main program is within y folder in c drive.
  • Package name in this program is x.
  • File name and package class name must be same ex: (number.java- source file).

How to compile package in java

c:\> javac folder/filename.java

c:\>java package.filename

Note: If we want to access all the classes of information then the classes are stored in different files with the same package name.

Sub Packages

In Java, it is possible to create a sub packages for the main packages.

Syntax

                 package  pack1.pack2;  

Where pack1 is the main package and pack2 is the sub package.

Java packages sub package example

Result

How many predefined packages in java

As per different java versions there are different packages and classes this SO question is also helpful.

We Inspected javase 14 and find there are 225 predefined packages / sub packages in java.

Which Package is default package in java

The package which is not required to import explicitly is known as default package.

If we write simple program to print hello then there we use System, String class we don’t need to import that. These are defined in java.lang package.

Default Package in Java is java.lang

Which keyword is used for accessing the features of a package

The import keyword is used for accessing the features of a package.

We can import classes and interfaces in our current file using import.

What is Import in Java

Import in a keyword in java. We use import statement to import class or all classes from a package.

How to import a package in java

import java.util.Date; or
import java.util.*;

How to create a package in java (Steps to create package in java with example)

  1. Create a folder with package name
  2. Create java files inside folder
  3. each java files include package definition package packagename;

packages in java with example program 

How to run package program in java (How to execute package in java)

compile d:\>javac foldername/filename.java
run d:\>java packagename.filename

What is java lang package in java

java lang (java.lang) is default package in Java. It contains fundamental classes.

Use of packages in java

  1. Group related classes and interfaces
  2. To avoid Naming conflicts
  3. To provide Access Control

Example of predefined packages in java

Predefined packages are defend by Java Team like

  1. java.lang (default package)
  2. java.util
  3. java.io
  4. java.net
  5. java.math
  6. java.sql
  7. java.time
  8. java.awt
  9. java.text

view all

The package which should be created to categorize the package further

The package which should be created to categorize the package further is sub package

java package java.

sub packages java.land, java.util etc.

Which of the following is used to avoid naming conflicts?

  1. class
  2. Interface
  3. package
  4. templet

Answer is package

Reference

Java Package