Last Updated:

Java packages with sample programs | Example

In addition, if different java files in different modules of the project have the same name, it is not possible to store two java files with the same name in the same folder because a name conflict can occur. This name conflict problem can be overcome by using the concept of packages.

In JavaAPI, they consist of one or more packages, where packages consist of many classes, classes contain several methods and fields. When you create a Java application, you should create the correct folder structure for better reuse, maintenance, and avoiding name conflicts, but how?

Package in Java

 

A package is nothing more than a physical folder (directory) structure that contains a group of related classes, interfaces, and subpackages according to their functionality.
This provides a convenient way to organize your work. The Java language has various built-in packages.

For example, java.lang, java.util, java.io, and java.net. All of these packages are defined as a very clear and systematized packaging mechanism for classification and management. Let's look at this with real-time examples.

Sample Java packages

 

A real example is when you download a movie, song or game, you create a separate folder for each category, for example, movie, song, etc. Similarly, a group of packages in java is similar to a library.


The classes and interfaces of a package are similar to workbooks in a library, which can be reused multiple times when we need them. This ability to reuse packages simplifies programming.

So when you create any software or application in the Java programming language, it contains hundreds or thousands of individual classes and interfaces. As such, they need to be organized into a meaningful package name to make proper sense, and reusing these packages in other programs may be easier.

The Advantage of Using Packages in Java

  1. Maintenance: Java packages are used for proper maintenance. If any developer has recently joined the company, they can easily get to the necessary files.
  2. Reusability: We can put shared code in a shared folder so that anyone can check that folder and use it if necessary.
  3. Name conflict: Packages help resolve a naming conflict between two classes with the same name. Suppose there are two classes with the same name Student.java. Each class will be stored in its own packages, such as stdPack1 and stdPack2, without any naming conflict.
  4. Organization: It also helps in organizing the files within our project.
  5. Access Protection: The package provides access protection. It can be used to control visibility. Class members can be defined so that they are visible only to members of that package.

Package Types in Java

 

There are two different types of packages in Java:

  • Custom Package
  • Predefined Package (Embedded Package)

Custom Package

A user-defined package is called a custom package. It contains custom classes and interfaces.

Creating a package in Java

Java supports the "package" keyword, which is used to create custom packages in Java programming. It has the following general appearance:

packageName

Here, packageName is the name of the package. The package statement must be the first line in the java source code file, followed by one or more classes.

For example:

packagemyPackage;
public class A {

class body

}

When designing a project, you must follow some naming conventions that apply tothe announcement of packages. Let's take an example to understand the convention. Below is the full structure of the project package.

Java packages
  1. Let's say you work at IBM and the domain name IBMwww.ibm.com. You can declare a package by changing the domain as follows:

    packagecom.ibm; where, com➝ This is usually the name of the company's specification, and the folder begins with com, which is called the root folder. ibm➝ The name of the company in which the product is being developed. It's a subfolder.

  2. hdfc➝ The name of the client for whom we are developing our product or working on a project.
  3. credit ➝ Project name.
  4. homeloan➝ This is the name of the modules of the loan project. There are several modules in the loan project, such as Mortgage Loan, Car Loan or Personal Loan. Let's say you work in a mortgage lending module.

This is a complete package structure, like a professional who is accepted in the company.

Another example:

packagecom.tcs.icici.loan.carloan.penalty;

Note: Keep in mind that the root folder must always be the same for all classes.

Predefined Packages in Java (Embedded Packages)

Predefined packages in java are those developed by SunMicrosystem. They are also called embedded packages in java. These packages consist of a large number of predefined classes, interfaces, and methods that are used by a programmer to perform any task in his programs. APIJava contains the following predefined packages, as shown in the figure below:

Java packages

How do I create a package in EclipseIDE?

In EclipseIDE, there are the following steps to create a java package. They are as follows:

  1. Right-click the src folder as shown in the photo below.
    Java packages
  2. Go to the new option and click on the package.
  3. A dialog box appears in which you must enter a name for the package according to the naming convention and click Finish.

After you create a package, a package folder will be created in your file system where you can create classes and interfaces.

Core Packages

  1. Java.lang: lang means language. A Java language pack consists of Java classes and interfaces that form the core of the Java language and the JVM. It is a fundamental package that is useful for writing and executing all Java programs. Examples are classes, objects, strings, streams, predefined data types, etc. It is automatically imported into Java programs.
  2. Java.io: io stands for input and output. It provides a set of I/O streams that are used to read and write data to files. A stream is a stream of data from one place to another.
  3. Javautil: util stands for utility. It contains a collection of useful utility classes and related interfaces that implement data structures such as LinkedList, Dictionary, Hash Table, Stack, Vector, Calendar, Data Utility, etc.
  4. Java.net: net stands for network. It contains network classes and interfaces for network operations. Client-server-related programming can be performed using this package.

Window Toolkit and Applet

  1. Java.awt: awt stands for abstractwindowtoolkit. Abstract window toolkit packages contain graphical user interface (GUI) elements such as buttons, lists, menus, and text areas. Programmers can develop programs with colorful screens, paintings, images, etc. with this package.
  2. Java.awt.image: It contains classes and interfaces for creating images and colors.
  3. Java.applet: It is used to create applets. Applets are programs that run from a server to a client computer on a network.
  4. Java.text: This package contains two important classes, such as DateFormat and NumberFormat. Use the DateFormat class to format dates and times. NumberFormat is used to format numeric values.
  5. Java.sql: SQL stands for Structured Query Language. This package is used in a Java program to connect databases such as Oracle or Sybase and extract data from them.

Key points

  • Predefined Java supports a package group that contains a group of classes and interfaces. These classes and interfaces consist of a group of methods. For example, the Java language contains a java.lang package that contains a string class, a StringBuffer class, a StringBuilder class, all wrapper classes, a executable interface, and so on.
  • Java contains 14 predefined packages, which are the main packages. These 14 predefined packages contain nearly 150 subpackets that consist of at least 7,000 classes. These 7,000 classes contain about 7 lakh methods.
  • Prior to Java 1.7, it contains 13 predefined packages. Starting with Java 1.8 onwards, one new package called java.time is introduced.

How do I view a list of predefined packages in Java?

  1. Follow these steps to view a list of predefined packages in Java.
  2. Navigate to the program files and open them.
  3. Now go to the Java folder and open it. You will see two folders such as the JDK and the JRE.
  4. Navigate to the JDK folder, extract the src folder. After extracting it, go to the Java folder. Here you will see 14 predefined package folders such as applet, awt, beans, io, lang, math, net, nio, rmi, security, sql, text, time and util.
  5. Now you open the lang package and scroll down. You can see classes like String, StringBuffer, StringBuilder, Thread, and so on.

Key points:

  • When importing another package, the first statement must be a package declaration followed by a package import.
  • A class can have only one package statement, but it can be multiple import package statements.
  • import can be written multiple times after a package statement and before a class statement.
  • You must declare the package with the name of the root folder (without the name of the subfolder), and the last file name must be a class name with semicolons.