Skip to content

Packages In Java Programming Language

In this article, you will get to know about the use of Packages in Java Programming Language. A package simply means a collection of objects encased in a box.

Similarly, in java programming language, a package is a collection of classes, interfaces, enumerations etc. that provides ease of access by eliminating naming variances.

Also Read:- Number Classes in Java Programming Language

TYPES OF JAVA PACKAGES

The java packages are mainly classified into two sections which are described below.

[1] Built-in/ Predefined Java Packages 

Java Programming Language provides several built-in packages consisting of several classes, input and output functions etc to enable different functionalities in a java program.

Here is a list of predefined core packages used in java programming language.

  • java.lang- contains basic classes that are essential in Java Programming Language
  • java.util- contains utility classes, legacy collection classes, frameworks, etc.
  • java.math- includes classes that performs BigInteger and BigDecimal arithmetics
  • java.io- contains classes for input and output functions
  • java.text- includes classes to manage texts, numbers, dates etc.

Some other important java packages are listed below.

  • java.text.spi
  • java.sql
  • java.security
  • java.net
  • javax.crypto
  • javax.xml
  • java.applet
  • java.awt
  • javax.swing
  • java.nio
  • javax.print
  • javax.accessibility
  • javax.imageio
  • javax.transaction
  • javax.management
  • java.beans

[2] User Defined Java Packages

Java programming language also provides support for creating user defined packages which can be utilised for different purposes such as :

  • eliminating naming variances to have ease of access,
  • encapsulating classes and interfaces to protect them from malicious activities by the outside means,
  • to organise classes or interfaces having similar kind of functionalities, and
  • to download a group of classes faster by compressing java packages into JAR files.

It may be noted that if a java package is left undeclared(without a package statement), a programmer would not be able to import the classes in the undeclared java package to a class in any other java package.

HOW TO CREATE, COMPILE AND RUN A JAVA PACKAGE

(A) Creating a Java Package

The keyword ‘package’ is used to create a java package followed by the name of the package and a semicolon at the end. A package statement must be declared at the beginning before creating a package.

Example:

// filename- W3TrainingSchool.java //
package coursedetails ;
class W3TrainingSchool {
public static void main(String [] args) {
System.out.println(“W3Training School Course Details”) ;
}
}

Output:

W3Training School Course Details

(B) Compiling a Java Package

A java package is compiled using the below syntax.

javac -d directory filename

Example:

The package ‘coursedetails’ with the package statement “// filename- W3TrainingSchool.java //” in the above example will be compiled in the following way.

javac -d W3TrainingSchool.java

Here, we have used a dot(.) in place of the directory name which shows that the package will be saved in the current directory. You can also change the path according to your need.

(C) Running a Java Package

To run a java package, the below syntax should be followed.

java package_name.class_name

Example:

java coursedetails.W3TrainingSchool

ACCESSING/ IMPORTING JAVA PACKAGES

You can access or import classes and interfaces of a java package into another java package by using any the following three import statements.

  1. import package_name.* ;
  2. import package_name.class_name ;
  3. fully qualified name of the class.

[1] Importing Java Packages using import package_name.* ;

Example:

// filename- W3TrainingSchool.java //
package courseA ;
class W3TrainingSchool {
public void digitalMarketing( ) {
System.out.println(“Digital Marketing Course”) ;
}
}

// filename- W3TS.java //
package courseB ;
import courseA.
class W3TS {
public static void main(String [] args) {
W3TrainingSchool  obj = new W3TrainingSchool( ) ;
obj.digitalMarketing( ) ;
}
}

Output:

Digital Marketing Course

[2] Importing Java Packages using import package_name.class_name ;

Example:

// filename- W3TrainingSchool.java //
package courseA ;
class W3TrainingSchool {
public void digitalMarketing( ) {
System.out.println(“Digital Marketing Course”) ;
}
}

// filename- W3TS.java //
package courseB ;
import courseA.W3TrainingSchool 
class W3TS {
public static void main(String [] args) {
W3TrainingSchool  obj = new W3TrainingSchool( ) ;
obj.digitalMarketing( ) ;
}
}

Output:

Digital Marketing Course

[3] Importing Java Packages using fully qualified name of the class.

Example:

// filename- W3TrainingSchool.java //
package courseA ;
class W3TrainingSchool {
public void digitalMarketing( ) {
System.out.println(“Digital Marketing Course”) ;
}
}

// filename- W3TS.java //
package courseB ;

class W3TS {
public static void main(String [] args) {
courseA.W3TrainingSchool obj = new courseA.W3TrainingSchool( ) ;
obj.digitalMarketing( ) ;
}
}

Output:

Digital Marketing Course

Also Read : String Classes in Java Programming

We have provided you the description on Packages used in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on Java Programming Language. 

Facebook
Twitter
LinkedIn
Pinterest