Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Chapter 4.

-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.

There are two types of package:

1. Built-in Package
2. User defined Package

Example of buit-in packages:

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”);
}
}

Organizing Classes and Interfaces in Packages

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:

To define a package, we have to compile a java program with package by using


following command:
javac -d . file_name.java

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");
}
}

Command: javac –d . Simple.java

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

Lets consider the following example:

1.Example of default access modifier:


In this example, we have created two packages pack and mypack. We are accessing the A class
from outside its package, since A class is not public, so it cannot be accessed from outside the
package.

File name: A.java

package pack;
class A{
void msg(){System.out.println("Hello");}
}

File name: B.java


package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}

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.

2. Example of public access modifier:

File name: A.java

package pack;
public class A{
public void msg(){System.out.println("Hello");}
}

File name: B.java

package mypack;
import pack.*;

class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}

Here in the above example, the output will be Hello.


As Class A and its method is public, it is accessible everywhere.

3.Example of Protected access modifier:

File name: A.java

package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}

File name: B.java

package mypack;
import pack.*;

class B extends A{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}

Here in the above example, the output will be Hello.


In this example, we have created the two packages pack and mypack. The A class of pack
package is public, so can be accessed from outside the package. But msg method of this package
is declared as protected, so it can be accessed from outside the class only through inheritance.

4.Example of Private access modifier:

File name: A.java

package pack;
private class A{
void msg(){System.out.println("Hello");}
}

File name: B.java


package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}

Here in the above example, Class A is Private, and private class cannot be inherited or cannot be
used through package import.

Making JAR Files for Library Packages

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.

we have to create manifest.txt folder in which we will write a package name as

Syntax: main-Class: packagename.


To make jar file of library package, we have to execute following command.

jar cfm jar-file input-file(s) pkgname/class1 pkgname/class2


………..

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

To make a JAR file of mypack package, I will execute following command.

jar cfm mypack.jar manifest.txt mypack/Simple.class mypack/Second.class


In this way , we can create a Jar file of library package.

Naming convention for packages:


● The prefix of a unique package name is always written in all-lowercase ASCII
letters and should be one of the top-level domain names, like com, edu, gov, mil, net, org.
● Subsequent components of the package name vary according to an organization’s own
internal naming conventions.

Import and static import

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 Import Statement

● 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:

import static java.lang.System.*; //Using Static Import


public class StaticImportDemo
{
public static void main(String args[])
{
//System.out is not used as it is imported using the keyword stati.
out.println("Welcome to Pune University");
}
}

Output:
Welcome to Pune University

You might also like