DEV Community

SILAMBARASAN A
SILAMBARASAN A

Posted on

Java Packages

A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories.

  • Built-in Packages (packages from the Java API)
  • User-defined Packages (create your own packages) The Java API is a library of prewritten classes, that are free to use, included in the Java Development Environment.

Built-in Packages

The library contains components for managing input, database programming, and much much more. The complete list can be found at Oracles website: https://docs.oracle.com/javase/8/docs/api/.

User-defined Packages

To create your own package, you need to understand that Java uses a file system directory to store them. Just like folders on your computer.

  • java.lang: This package holds the foundational classes.
  • java.io: It handles all sorts of input and output operations.
  • java.awt: This package provides the basic building blocks for creating graphical user interfaces (GUIs).
  • javax.swing: It is best for more advanced and richer desktop application GUIs due to its comprehensive toolkit.
  • java.net: It contains classes essential for developing network-aware applications.
  • java.util: It is a versatile package filled with utility classes, including the powerful Collections Framework.
  • java.applet: This package includes classes for building applets, which are small applications designed to run in web browsers (though less common now).
  • java.sql: Here you will find the classes for interacting with databases and processing relational data. above the all to be discussed.

User-defined packages

User-defined packages in Java are not readily available, as their name suggests. These are created by developers to define interfaces and classes according to the application requirements. It is defined using a unique syntax as shown below:


package packageName;
Enter fullscreen mode Exit fullscreen mode

You can see the package keyword in its syntax, it defines the package name. It is important to declare the package just before any import statements of a Java class. It is also important to ensure that all the classes are public so that you can access them from both inside and outside the package.

Top comments (0)