Professional Documents
Culture Documents
Chapter 4 Package
Chapter 4 Package
-Package
A package is a collection of similar types of Java entities such as classes, interfaces, subclasses,
exceptions, errors, and enums. A package can also contain sub-packages.
It is like a folder or directory in which we are storing files or subdirectories so that they can
easily maintain.
Advantages:
1. As classes and interfaces are separated in a package, it becomes easy to search and locate.
2. It makes data encapsulation(hiding) possible.
3. It avoids naming conflict, for ex: suppose we need two Student classes which we have to
use to deal with the college’s students information as well as School’s students information. In
this case if we both the classes are having the same name then there may be an ambiguity. Two
avoid naming conflicts we can create separate packages by name college and school and we’ll
put the respected student class in the respected package.
4. Reuse the classes contained in the packages of other programs.
1. Built-in Package
2. User defined Package
java.lang: It contains classes for primitive types, strings, math functions, threads, and
exceptions.
java.util: It contains classes such as vectors, hash tables, dates, Calendars, etc.
java.io: It has stream classes for Input/Output.
java.awt: Classes for implementing Graphical User Interface – windows, buttons, menus, etc.
java.net: Classes for networking
java. Applet: Classes for creating and implementing applets
Syntax:
package packageName;
Class className
{
Public static void main(Strinf args[])
{
System.out.println(“package creation example”);
}
}
While creating a package, programmers must choose a name for the package and include a
package statement along with that name at the top of the source program that contains the
classes, interfaces, enumerations, and annotation types that you want to include in the package.
There can be only one statement of the package in each source code, and it applies to all types in
the file.
Defining Package:
After that, a folder is created with the given package name in a specific location,
and the compiled class files will be placed in that folder.
Example :
package mypack;
public class Simple
{
public void display()
{
System.out.println("Welcome to package");
}
}
After executing above command, one package(directory will get create by name mypack, and
one class file by name Simple.class will get store in it.
Package as access protection:
In java, the package is a container of classes, sub-classes, interfaces, and sub-packages. The class
acts as a container of data and methods. So, the access modifier decides the accessibility of class
members across the different packages
package pack;
class A{
void msg(){System.out.println("Hello");}
}
Here in the above example, class A is in package pack and class B is in package mypack as
class A is default, so it cannot be accessed from outside the package.
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
package pack;
private class A{
void msg(){System.out.println("Hello");}
}
Here in the above example, Class A is Private, and private class cannot be inherited or cannot be
used through package import.
The Java™ Archive (JAR) file format enables you to bundle multiple files into a single archive
file. Typically a JAR file contains the class files and auxiliary resources associated with applets
and applications.
Making JAR Files for Library Packages means we have to create a library of package. The
process is same for making jar file of classes and packages.
Option Description
Produces verbose output on stdout while the JAR file is being built. The verbose output
v
tells you the name of each file as it's added to the JAR file.
0 (zero) Indicates that you don't want the JAR file to be compressed.
M Indicates that the default manifest file should not be produced.
Used to include manifest information from an existing manifest file. The format for
using this option is:
jar cmf jar-file existing-manifest input-file(s)
See Modifying a Manifest File for more information about this option.
m
Warning: The manifest must end with a new line or carriage return. The last line will
not be parsed properly if it does not end with a new line or carriage return.
-C To change directories during execution of the command. See below for an example.
The f option indicates that you want the output to go to a file rather than to stdout.
f
Example:
Suppose I have mypack package which contains two classes as
1. Second.class
2. Simple.class
Then create file manifest.txt and write a package name into it as shown below.
main-Class: mypack
import statement
● To access a class or method from another package we need to use the fully qualified
name or we can use import statements.
● The class or method should also be accessible. Accessibility is based on the access
modifiers.
● Private members are accessible only within the same class. So we won't be able to access
a private member even with the fully qualified name or an import statement.
● The java.lang package is automatically imported into our code by Java.
Example:
import java.util.Vector;
public class ImportDemo
{
public ImportDemo()
{
//Imported using keyword, hence able to access directly in the code without package qualification.
Vector v = new Vector();
v.add("Tutorials");
v.add("Point");
v.add("India");
System.out.println("Vector values are: "+ v);
//Package not imported, hence referring to it using the complete package.
java.util.ArrayList list = new java.util.ArrayList();
list.add("Tutorix");
list.add("India");
System.out.println("Array List values are: "+ list);
}
public static void main(String arg[])
{
new ImportDemo();
}
}
Output:
Vector values are: [Tutorials, Point, India]
Array List values are: [Tutorix, India]
● Static imports will import all static data so that can use without a class name.
● A static import declaration has two forms, one that imports a particular static member
which is known as single static import and one that imports all static members of a class which
is known as a static import on demand.
● Static imports introduced in Java5 version.
● One of the advantages of using static imports is reducing keystrokes and re-usability.
Example:
Output:
Welcome to Pune University