Training Outcomes Within Your Budget!

We ensure quality, budget-alignment, and timely delivery by our expert instructors.

Share this Resource

Table of Contents

Packages in Java

Created by accident, Java was produced as a result of cleaning up the C++ code while building a set-top box. A language freed from the concept of pointers as their presence compromises the security, increasing the complexity of the language in the process. Going deeper, one element, which aims to control access and prevent naming conflicts, is referred to as a Package in Java.

A Package is a mechanism in Java utilised to encapsulate class groups, interfaces and sub-packages. Check out this blog on Packages in Java, explaining What it is and providing a complete tutorial on creating, importing, and compiling packages. 

Table of Contents

1)  What are Packages in Java?

2) Working mechanism of Packages in Java

3) Examples of Packages in Java

  a) Built-in Packages

  b) User-defined Packages

4) Conclusion

What are Packages in Java?

A Package in Java is purposed for grouping classes related to one another. Packages can be considered a directory for files and are implemented to avoid naming conflicts and produce better-quality code in maintenance. 

A generally followed protocol is to group related classes into a Package, then write an import class from available Packages such as ‘import java.util.*’. In this statement, the asterisk symbol, the ‘wildcard’, is included to import all classes in the ‘util’ package. 

An advantage of Package classes is that they can also be reused as many times as desired in our program. The names of Packages are very similar to the directory structure, such as if a package name is ‘city.people.area’, there exist three directories, namely ‘city’, ‘people’ and ‘area’. Additionally, the directory ‘city’ is accessible using the ‘CLASSPATH’ variable because the parent directory’s path of ‘city’ exists inside CLASSPATH. The central idea here is to ensure that classes are easily locatable.
 

Java Training 
 

Working mechanism of Packages in Java

In Java, Packages play a crucial role in organising and categorising classes, allowing for better code management. The syntax for referencing classes within Packages follows a hierarchical structure, exemplified by "Package_name.sub_package_name.class_name."

Built-in Packages in Java

Java's built-in Packages, such as java.awt.event, demonstrate the hierarchical structure. For instance, in the Package java.awt.event, 'java' is the Package name, 'awt' is the subpackage, and 'event' is the class name.

User-defined Packages in Java

Creating user-defined Packages involves understanding the Java file system directory. For instance, 'University.Department.Staff' signifies the 'University' as the Package name, 'Department' as the subpackage, and 'Staff' as the class name.

Importing Packages in Java

To utilise classes or Packages, the 'import' keyword is essential. One can import a specific class from a Package, an entire package, or use the complete qualified name without importing.

Example 1: Importing Built-in Packages
 

import java.util.*;  

import java.util.Scanner;

 

public class PackageImportExample {

    public static void main(String args[]) throws Exception {

        Scanner scanObj = new Scanner(System.in);

        System.out.print("Enter Your UserName:");

        String sName = scanObj.nextLine();

        System.out.println("UserName is: " + sName);

        scanObj.close();

 

        Date currdate = new Date();

        System.out.println(currdate.toString());

 

        java.net.InetAddress ipAddress=java.net.InetAddress.getLocalHost();

        System.out.println("My IP Address :"+ipAddress.getHostAddress());                     

    }

}

 

Example 2: Creating user-defined Packages
 

package university;

 

public class WelcomeMessage {

    public void ShowMessage() {

        System.out.println("Welcome to our University");

    }

}

 

Example 3: Creating sub-packages
 

package university.department;

 

public class DepartmentGoal {

    public void DepartmentGoalMessage() {

        System.out.println("Department Message Displayed");

    }

}

 

Example 4: Creating Classes in Sub-packages
 

package university.department;

public class Staff {

    public void AddStaff() {

        System.out.println("Staff Added!");

    }

    public void RemoveStaff() {

        System.out.println("Staff Removed!");

    }

}

 

Example 5: Using user-defined Packages in Java Program:
 

import university.department.Staff;

public class MyOwnPackageExample {

    public static void main(String args[]) {

        Staff mystaff = new Staff();

        mystaff.AddStaff();

        mystaff.RemoveStaff();

    }

}

 

Step-up your existing Java knowledge and design your own websites and applications. Sign up for our Java Courses now!

Examples of Packages in Java

Built-in Package

The Java API is a library containing prewritten classes that are free and included within the Java Development Environment (JDE). This library is split into classes and packages, meaning that either one class can be imported or a complete Package that includes all the classes belonging to the specified Package. 

To use a Package or class from the Java library, the 'import' keyword must be used in the following way:

To import one single class: import package.name.class;

To import the complete package: import package.name.*;

User-defined Packages

To make your Package, it must be understood that Java has a file system directory for storage, similar to the folder system in your computer. To create your Package, just use the 'package' keyword. For example, 'package mypackage;'.

Packages

Conclusion

You have been introduced to the concept of a Package in Java so far. This blog has briefly illustrated the classes involved and the protocol for importing Packages. You have also learnt that Java provides you with built-in and the ability to create your Packages. Packages in Java essentially resemble the file directory structure in a computer system and enable access to public classes in other packages.

Get in-depth training in Java programming and learn to program for your organisation’s benefit. Sign up for the Java Programming Course now!

Frequently Asked Questions

Why are packages important in Java programming? faq-arrow

Packages in Java are crucial for organising and structuring code, preventing naming conflicts, and enhancing code readability. They facilitate modular development, code reuse, and effective management of large-scale projects.

What is Java Util Package? faq-arrow

The Java Util Package is a fundamental Package in Java that provides utility classes and interfaces for basic data structures, date and time handling, input/output operations, and more. It includes commonly used functionalities, such as collections, arrays, and the Date class, making it a vital component of Java programming for diverse applications.

What is Knowledge Pass, and how does it work? faq-arrow

The Knowledge Academy’s Knowledge Pass, a prepaid voucher, adds another layer of flexibility, allowing course bookings over a 12-month period. Join us on a journey where education knows no bounds.

What are related Courses and blogs provided by The Knowledge Academy? faq-arrow

The Knowledge Academy offers various Java Trainings, including Java Programming, JavaScript for Beginners and Java Swing Development Training. These courses cater to different skill levels, providing comprehensive insights into How to Become a Java Developer.  

Our Programming and DevOps blogs cover a range of topics related to Java, offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your Java Programming skills, The Knowledge Academy's diverse courses and informative blogs have you covered.
 

What are the other resources provided by The Knowledge Academy? faq-arrow

The Knowledge Academy takes global learning to new heights, offering over 30,000 online courses across 490+ locations in 220 countries. This expansive reach ensures accessibility and convenience for learners worldwide. 

Alongside our diverse Online Course Catalogue, encompassing 17 major categories, we go the extra mile by providing a plethora of free educational Online Resources like News updates, Blogs, videos, webinars, and interview questions. Tailoring learning experiences further, professionals can maximise value with customisable Course Bundles of TKA.

Upcoming Programming & DevOps Resources Batches & Dates

Date

building Java Programming

Get A Quote

WHO WILL BE FUNDING THE COURSE?

cross

OUR BIGGEST SPRING SALE!

Special Discounts

red-starWHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.