Packages

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Packages

 A Package can be defined as a grouping of related types (classes,


interfaces, enumerations and annotations) providing access
protection and namespace management.

 We can use classes from other programs without physically copying


them into the programs under development.

 Package is another way of achieving the re-usability in java.

 We have two types of packages in Java: built-in packages and the


packages we can create (also known as user defined package).

 Because software written in the Java programming language can be


composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and
interfaces into packages.

Advantage of packages:
 Reusability: While developing a project in java, we often feel
that there are few things that we are writing again and again in
our code. Using packages, you can create such things in form of
classes inside a package and whenever you need to perform that
same task, just import that package and use the class.

 Better Organization: Again, in large java projects where we have


several hundreds of classes, it is always required to group the
similar types of classes in a meaningful package name so that you
can organize your project better and when you need something you
can quickly locate it and use it, which improves the efficiency.
 Name Conflicts: We can define two classes with the same name in
different packages so to avoid name collision, we can use
packages.

Types of packages in Java


 User defined package: The package we create is called user-defined
package.
 Built-in package: The already defined package
Example:
java.io.*;
java.lang.*;
java.util.Scanner ;
java.awt.*;
etc are known as built-in packages

for example, when we need user input, we import a package like this:
import java.util.Scanner;
Here:
→ java is a top-level package
→ util is a sub package
→ and Scanner is a class which is present in the sub package util.

Naming conventions:
 According to standard java naming rules, packages begin with
lowercase letters. This makes it easy for users to distinguish
package names from class names.

 Every package name must be unique to make the best use of


packages. Duplicate names will cause run-time errors. Duplicate
package names are un-avoidable since multiple users work on
Internet.

 Therefore, java designers suggested a package naming convention


that ensures uniqueness. Since internet domain names are unique,
it is preferred using domain names in reverse order and prefix to
the preferred pack names.

 for example, if my domain name is www.krishnaa.com, and I want to


create a package with the name math, I can create my package as
follows:
com.krishnaa.math
Creating a package:
 we use package keyword to create a package. For example
package pack1;
public class Test
{
public static void main(String[] args)
{
System.out.println("Welcome to packages in java");
}
}
To compile:
javac –d . Test.java

 The -d switch specifies the destination where to put the generated


.class file.
 You can use any directory name like d:/abc. If you want to keep
the package within the same directory, you can use . (dot)
To run the program:
Java pack1.Test

import keyword is used to import packages in java. Following are the


various examples to import a package in java.
import java.util.*;
Above imports all classes and interfaces available in util package.
But does not import the calsses and interfaces available in a sub-
package inside the util package.
import java.util.Scanner;
Above statement, imports only the Scanner class from util package.
Program-1
package maths;
public class Calc
{
public int add(int x, int y)
{
return (x+y);
}
public int diff(int x, int y)
{
return (x-y);
}
public double prod(int x, int y)
{
return (x*y);
}
}
Program-2

package mypack;
import maths.*;

class Test
{
public static void main(String args[])
{
Calc obj = new Calc();
int sum = obj.add(10,20);
System.out.println("sum= "+sum);
}
}

Example for package usage:


To compile the program:
javac –d . *.java

To run the program:


java mypack.Test

Output: sum=30
difference = 10

Sub-package in java
Package inside the package is called the sub-package. It should be
created to categorize the package further.

package mypack.mp1.mp2.mp3;
import maths.*;
class Test
{
public static void main(String args[]){
Calc obj = new Calc();

int sum = obj.add(10,20);


System.out.println("sum= "+sum);
int diff = obj.diff(20,10);
System.out.println("difference = "+diff);
double prod = obj.prod(10,20);
System.out.println("Product = "+prod);
}
}
To compile:
javac –d . Test.java

To run:
java mypack.mp1.mp2.mp3.Test

Output:

How to send the class file to another directory or drive?


There is a scenario, I want to put the class file of A.java source
file in classes folder of c: drive. For example:

To Compile:
e:\sources> javac -d c:\classes Simple.java
To Run:
To run this program from e:\source directory, you need to set
classpath of the directory where the class file resides.

e:\sources> set CLASSPATH=c:\classes;.;


e:\sources> java mypack.Simple

Setting CLASSPATH to avoid fully qualified names


Can be done in two ways:
1. Temporary:
 By setting the CLASSPATH in the command prompt
 By –classpath/-cp switch

2. Permanent:
 By setting the CLASSPATH in the environment variables
 By creating the jar file, that contains all the class files, and
copying the jar file in the jre/lib/ext folder.

What is CLASSPATH?
The CLASSPATH is an environment variable that tells the java compiler
where to look for class files to import. CLASSPATH is generally set
to a directory or a JAR (Java Archive) file.

To see the currently set calsspath in the command prompt user the
following command: c:\> echo %CLASSPATH%
The JAR Files
 A JAR (java archive) file is that contains compressed version of
.class files, audio files, image files or directories.

 We can imagine a .jar file as zipped file (.zip) that is created


by using WinZip software. JAR file is useful to bundle up several
files related to a project and use them easily.

 To create .jar file in java we use jar command in the following


ways:
1. jar cf maths.jar maths – in this command, cf represents
create file. And maths package will be converted into
maths.jar.

2. jar tf maths.jar – in this command, tf represents table view


of file maths.jar.
The first two entries represent that there is a manifest file
created and added to maths.jar file. The third entry represents
the sub directory with the name maths and the last two represent
the file names in the directory maths.

When we create a .jar file, it automatically receives the


default manifest file. There can be only one manifest file in an
archive, and it always has the pathname.

Set the CLASSPATH permanently to the maths.jar. let us assume that


maths.jar is in the subdirectory pack. Follow the steps:

Step-1: Right click on This Pcselect Properties from pop-up.


Step-2:

Step-3:
Step-4:

Step-5:

Step-6: click ok 3 time in all previously opened windows, your


CLASSPATH is set.
* * *

You might also like