Search This Blog

Saturday, July 10, 2021

What are Packages in core java and how to use them ?

  Java package is a technique for organizing Java classes into namespaces similar to the modules of Modula, providing modular programming in JavaJava packages can be stored in compressed files called JAR files, allowing classes to be downloaded faster as groups rather than individually.

Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.

A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and namespace management.

Some of the existing packages in Java are::

·        java. lang - bundles the fundamental classes

·        java.io - classes for input, output functions are bundled in this package

Programmers can define their own packages to bundle groups of classes/interfaces, etc. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, annotations are related.

Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classes.

Creating a package:


When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.

 

What are Packages in java and how to use them?

 OOPS CONCEPT

Packages in Java are a mechanism to encapsulate a group of classes, interfaces, and sub-packages. Many implementations of Java use a hierarchical file system to manage source and class files. It is easy to organize class files into packages. All we need to do is put related class files in the same directory, give the directory a name that relates to the purpose of the classes, and add a line to the top of each class file that declares the package name, which is the same as the directory name where they reside.

In java, there are already many predefined packages that we use while programming.

For example: java.langjava.iojava.util etc.


However one of the most useful features of java is that we can define our own packages


Advantages of using a package:


Before discussing how to use them Let see why we should use packages.

§  Reusability:  Reusability of code is one of the most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be reused by any number of programs wishing to incorporate the class in that particular program.

§  Easy to locate the files.

§  In a real-life situation, there may arise scenarios where we need to define files of the same name. This may lead to “name-space collisions”. Packages are a way of avoiding “name-space collisions”.


Types of packages:


1) User-defined package: The package we create is called a user-defined package.
2) Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.


Defining a Package:


This statement should be used at the beginning of the program to include that program in that particular package.


package  <package name>;

 

No comments:

Post a Comment

Submit a Comment