0% found this document useful (0 votes)
6 views15 pages

11 - Packages

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Packages

Packages
Learning Objectives

 Defining Packages
 Package Scope
 The import Statement
 Static Imports
 CLASSPATH and Import
What is Package?
Packages in Java is a mechanism to encapsulate a
group of classes, interfaces and sub packages.

Many implementations of Java use a hierarchical file


system to manage source and class files
Advantages of using a package
 Reusability

 Easy to locate the files.

 Packages are a way of avoiding “name-space


collisions”.

 Java package provides access protection.


Types of Packages
1) Built-in package

2) User defined package


Steps for Creating package
 Choose the appropriate package name, the package
name must be a JAVA valid variable name.

 The package statement must be first executable


statement.

 Choose the appropriate class name or interface name


and whose modifier must be public.

 The modifier of the methods of class name or interface


name must be public.

 At any point of time either place a class or an interface in


a package and give the file name as class name or
interface name with extension .java
Example of package
package mypack
class Book
{
String bookname;
String author; Book(String b, String c)
{
this.bookname = b;
this.author = c;
}
public void show()
{
System.out.println(bookname+" "+ author);
}
}
Example of package
class Test
{
public static void main(String[] args)
{
Book bk = new Book("java","Herbert"); bk.show();
}
}

Points to remember:
At most one package declaration can appear in a
source file.

The package declaration must be the first statement in


the unit.
Steps for Compiling a Package
program
 Create a directory under your current working
development directory(i.e. JDK directory), name it
as your specified package name(ex : mypack)
 Compile the source file
 Put the class file into the directory you have
created.
 Execute the program from development directory.
 Syntax for compiling a package:
javac –d . filename.java
For example:
javac –d . Test.java
Naming Convention
 Package names are written in all lower case to avoid
conflict with the names of classes or interfaces.

 Companies use their reversed Internet domain


name to begin their package names—
 for example, com.example.mypackage for a
package named mypackage
import keyword
 import keyword is used to import built-in and user-defined
packages into your java source file. So that your class can
refer to a class that is in another package by directly using
its name.
 There are 3 different ways to refer to class that is present
in different package

 Using fully qualified name


class MyDate extends java.util.Date { //statement; }
import the only class you want to use.
import java.util.Date;
class MyDate extends Date { //statement. }
import all the classes from the particular package
import java.util.*;
class MyDate extends Date { //statement; }
Using static import
 There are situations where you need frequent
access to static final fields (constants) and static
methods from one or two classes. Prefixing the
name of these classes over and over can result in
cluttered code.
 The static import statement gives you a way to
import the constants and static methods that you
want to use so that you do not need to prefix the
name of their class.
 Example:
import static java.lang.Math.PI;
CLASSPATH
 The CLASSPATH environment variable tells the Java
Virtual Machine and other Java applications where to find
the class libraries, including user-defined class libraries.

 The CLASSPATH environment variable is set with the


set command.
set CLASSPATH=path1;path2 ...
set CLASSPATH=C:\java\MyClasses

 The paths should begin with the letter specifying the


drive, for example, C:\...
Interview Questions
 What is a Java package and how is it used?

 How the concept of packages achieves access-protection?

 Which package is always imported by default?

 Does importing a package imports the sub packages as


well?

 What are the practical benefits, if any, of importing a specific


class rather than an entire package (e.g. import java.util.*
versus import import java.util.Arrays)?
Any Questions

You might also like