A package in Java is used to group a set of related classes and interfaces. In a big Java application containing hundreds or thousands of individual classes, grouping related classes and interfaces into packages helps to keep things organized.
Package structure in Java
Packages follow the same structure as directories in OS where you have a root directory which may contain zero or more sub-directories and then there are files. Same way with in a package structure there is a top level package which may have subpackages and classes.
For example- java.util.ArrayList
Here top level package is – java
Containing a subpackage- util
Which in turn contains the class - ArrayList
Advantages of using package in Java
Package in Java provides following advantages-- Helps with organizing classes- Packages are used to organize set of related classes and interface which makes its easy to find a specific class, interface. If you want to use specific class residing in a package in another class you can import class from that package and use it.
- Prevent name collision- Packages in Java also prevent naming collision. You can have class with same name in different packages. For example com.knpcode.finance.MyClass and com.knpcode.accounts.MyClass.
- Provides access control- Packages also help in controlling the visibility of the members.
Access specifiers
protected and default have visibility with in a package.
- A member having default access is visible only within its own package.
- A member having protected access can only be accessed within the package it belongs to or by a subclass of its class in another package.
Types of packages in Java
In Java programming language there are two types of packages.- Built-in packages- Java comes pre-packaged with many built-in packages containing classes
that are grouped together as per the functionality.
For example-
- java.lang- Contains fundamental classes. This package is automatically imported.
- java.io- Contains classes for reading and writing (input and output).
- java.util- Contains the collections framework, date and time facilities, internationalization, and miscellaneous utility classes.
- User defined packages- You will use API classes contained in these built-in packages with in your Java application but to group classes with in your application you’ll create user defined packages.
Creating a package in Java
To create a package, you put a package statement with package name at the top of every source file (class, interface, enumerations, and annotation types) that you want to include in the package.
Syntax for package creationpackage package_name;
Package creation example
package com.knpcode.programs; public class MyClass { public static void main(String[] args) { System.out.println("creating a package"); } }
In the example com.knpcode.programs package structure is created if it does not exist already. If package already exists then class MyClass is included with in that package.
Few points to keep in mind while creating a package are-
- package statement must be the first line in the source file.
- There can be only one package statement in each source file.
Sub-packages in Java
As already discussed in the beginning a top level package may contain another package which is called the sub-package.
By using sub-packages you can segregate files based on sub-functionalities. A top level package for broader functionality and sub-packages containing source files grouped by sub-functionalities.
A hierarchy of packages is created by separating package names by a period (.).
For example- com.knpcode.programs
Here programs is a package inside knpcode which is under com.
Importing packages in Java
If you want to use specific source file (class, interface) residing in one package from outside its package, you must do one of the following:
- Import the package member
- Import the member's entire package
- Refer to the member by its fully qualified name
Import the package member
To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement.
For example if you are using HashMap class in your class than you do need to import HashMap class from java.util package.
package com.knpcode.programs; import java.util.HashMap; import java.util.Map; public class MyClass { public static void main(String[] args) { Map<String, String> testMap = new HashMap<String, String>(); } }
Import the member's entire package
Rather than importing specific classes you can import the whole package.
import java.util.*;
Refer to the member by its fully qualified name
Rather than using import statement you can also use fully qualified name. For example if you are using HashMap class in your class then you can use fully qualified name.
package com.knpcode.programs; public class MyClass { public static void main(String[] args) { java.util.Map<String, String> testMap = new java.util.HashMap<String, String>(); } }That's all for the topic Package in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment