In this video you will learn that what is package in java, how to create package and use package.
Package in java
A package in java is an encapsulation mechanism that can be used to group related classes, interfaces, enums, and subpackages. The below diagram shows a package hierarchy, comprising a package called java that contains two other packages; io and awt. The package io has a class InputStream that implements the interfaces Closeable and AutoCloseable, also found in the same package. The package awt has two classes Color and Frame.
The dot (.) notation is used to uniquely identify package members in the package hierarchy. The class java.io.InputStream is different from the class java.awt.Color. The Frame class can be easily identified by java.awt.Frame. This is called the fully qualified name of the type.
Java programming environment usually map the fully qualified name of packages to the underlying file system. For example, on a window system, the class Frame.class corresponding to the fully qualified name java.awt.Frame would be found under the folder java/awt.
Defining packages
A package hierarchy represents an organization of the java classes and interfaces. It does not represent the source code organization of the classes and interfaces. The source code is of no consequence in this regard. Each java source file (also called compilation unit) can contain zero or more type declarations, but the compiler produces a separate .class file containing the java byte code for each of them. A type declaration can indicate that its java byte code be placed in a particular package, using a package declaration.
The package statement has following syntax :
package fullyqualifiedpackagename ;
At most one package declaration can appear in a source file, and it must be the first statement in the source file. The package name is saved in the java byte code for the types contained in the package.
Note that this scheme has two consequences. First, all the classes and interfaces in a source file are placed in the same package. Second, several source files can be used to specify the contents of the package.
If a package declaration is omitted in a compilation unit, the java byte code for declarations in the compilation unit will belong to an unnamed package (also called the default package) , which is typically synonymous with the current working directory on the local host system.
The following example to add classes into a package:
package MyPackage;
public class MyMaths
{ public int sum(int ... x)
{
int s=0;
for(int n:x)
s+=n;
return s;
}
}
Using packages:
The import facility in java makes it easier to use the contents of packages. The accessibility of types in a package determines their access from other packages. Given a reference type that is accessible from outside a package, the reference type can be accessed in two ways. One way is to use the fully qualified name of the type. However, writing long names become tedious. The second way is to use the import declaration that provide a shorthand notation for specifying the name of the type, often called type import. The import declaration must be the first statement after any package declaration in a source file. The simple form of the
import declaration has the following.
Syntax:
import fullyqualifiedtypename ;
This is called single-type-import. As the name implies, such an import provide a shorthand notation for a single type. The name of the type can now be used to access this particular type. Given the following import declaration:
import MyPackage.MyMath;
The simple name MyMath can be used in the source file to refer to this class.
Alternatively, the following form of import declaration can be used:
import fully-qualified-package-name .*;
This is called type-import-on-demand. It allows any type from the specified package to be accessed by its simple name.
An import declaration does not recursively import sub packages. The declaration also does not result in inclusion of source code of the types. The declaration only imports type names.
All compilation units implicitly import the java.lang package. This is the reason why we can refer to the class String by its simple name, and need not use its fully qualified name java.lang.String all the time.
www.tarunsir.com
www.cinstitute.org.in
Connect me on linkedin
/ tarrunverrma
Connect me on instagram
/ the_ultimate_coding_stuff
Follow me on twitter
/ tarrunverrma
Join my telegram channel
t.me/tarunvermasir
#tarunsir #javatutorial #learnprogramming #coding #java #interface
#package #import