Package

Java Packages & API:

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)

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.

User-defined Packages: [TBD]

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

Subpackage in java:[TBD]

Package inside the package is called the subpackage. It should be created to categorize the package further.

What does API stand for?[TBD]

API stands for Application Programming Interface. In the context of APIs, the word Application refers to any software with a distinct function. Interface can be thought of as a contract of service between two applications. This contract defines how the two communicate with each other using requests and responses. Their API documentation contains information on how developers are to structure those requests and responses.

Advantage of Java Package:

1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

How to access package from another package?

There are three ways to access the package from outside the package.

  1. import package.*;
  2. import package.classname;
  3. fully qualified name.

1) Using packagename.*[TBD]

If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.

The import keyword is used to make the classes and interface of another package accessible to the current package.

2) Using packagename.classname

If you import package.classname then only declared class of this package will be accessible.

3) Using fully qualified name [TBD]

If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.

Reference:

https://www.w3schools.com/java/java_packages.asp

https://aws.amazon.com/what-is/api/#:~:text=API%20stands%20for%20Application%20Programming,other%20using%20requests%20and%20responses.

https://www.javatpoint.com/package#:~:text=A%20java%20package%20is%20a,io%2C%20util%2C%20sql%20etc.

Leave a comment

Design a site like this with WordPress.com
Get started