DEV Community

Sakthi S
Sakthi S

Posted on

Java Packages?

Packages

  • A packages in java is used to group related classes.Think of it as a folder in a file directory.And to write a better maintainable code.

  • Providing access control using public, protected, and default access.

  • It Avoiding name conflicts (two classes with the some name can exist in different packages).

Packages are divided into two categories:

  1. Built-in packages
    (packages from the Java API)

  2. User-defined Packages
    (create your own packages)

Built-in packages

  • The Java API is a library of prewritten classes, that are free to use, included in the Java Development Environment.

  • The library contains components for managing input, database programming, and much much more.

  • The library is divided into packages and classes. Meaning you can either import a single class (along with its methods and attributes), or a whole package that contain all the classes that belong to the specified package.

Syntax:

import package.name.Class;   // Import a single class
import package.name.*;   // Import the whole package
Enter fullscreen mode Exit fullscreen mode

User-defined Packages

  • User-defined packages are those packages that are designed or created by the developer to categorize classes and packages.

  • It can be imported into other classes and used in the same way as we use built-in packages.

  • But if we omit the package statement, the class names are put into the default package, which has no name.

Syntax:

package package-name;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)