OOEDP_wk2_L2

download OOEDP_wk2_L2

of 9

Transcript of OOEDP_wk2_L2

  • 8/3/2019 OOEDP_wk2_L2

    1/9

    OOEDP Week 2 Lecture 2 1January 12

    Introduction to Packages

    Object Oriented and Event Driven Programming

    Week 2 Lecture 2

  • 8/3/2019 OOEDP_wk2_L2

    2/9

    OOEDP Week 2 Lecture 2 2January 12

    Objectives

    In this chapter, we will

    Introduce Packages

    Discuss the location and naming of packages

  • 8/3/2019 OOEDP_wk2_L2

    3/9

    OOEDP Week 2 Lecture 2 3January 12

    Packages in Java

    What is a Package?

    Why do we use Packages?

    Where are Packages stored?

    How are Packages located?

  • 8/3/2019 OOEDP_wk2_L2

    4/9

    OOEDP Week 2 Lecture 2 4January 12

    Packages

    Packages are used to group classes in a meaningful way

    There are four main reasons why packages are used:

    To avoid name conflicts

    To simplify locating classes To distribute software conveniently

    To control access to protected members of classes

    The import statement is used to provide access to classesin a package:

    import java.util.*;

    import javax.swing.JOptionPane;

  • 8/3/2019 OOEDP_wk2_L2

    5/9

    OOEDP Week 2 Lecture 2 5January 12

    Packages and Directories

    In Java a package is a directory that contains thebytecode of the classes in the package

    Packages can be hierarchical; that is packages within

    packages This is reflected in the directory structure

    For example I may wish to store the Card class in apackage called cardgames that is in a package calledmypackages in a package called pete

    pete.mypackages.cardgames.Card

    The class would be held in a directory structure such as:

    C:\pete\mypackages\cardgames

  • 8/3/2019 OOEDP_wk2_L2

    6/9

    OOEDP Week 2 Lecture 2 6January 12

    Packages and CLASSPATH

    The directory that a package exists in does not have to beat the root level

    However, the java tools need to be able to find the

    package If the package is not at the root level then it is necessary

    to set CLASSPATH to allow the tools to find the package

    CLASSPATH is an environment variable

    In windows

    Modify the environment variables via control panel

    In unix use the setenv command

  • 8/3/2019 OOEDP_wk2_L2

    7/9

    OOEDP Week 2 Lecture 2 7January 12

    Adding Classes to a Package

    The keyword package is used to indicate that a classbelongs to a certain package

    For example:

    package pete.mypackages.cardgamespublic class Card

    {

    ....

    The keyword package must be the first statement in a file

    Apart from blank lines or comments

    The package should be stored in an appropriate directory

    Other classes can be added to the same package

    One public class per file

  • 8/3/2019 OOEDP_wk2_L2

    8/9

    OOEDP Week 2 Lecture 2 8January 12

    Naming Packages

    Package name should be unique

    The convention suggested is to use internet domainnames in reverse order as a package prefix

    Remember internet domain names are unique For example:

    Consider the domain www.fcet.staffs.ac.uk

    This could be used as a package prefix:

    uk.ac.staffs.fcet.pete.mypackages.cardgames

    This makes directory organisation fun

  • 8/3/2019 OOEDP_wk2_L2

    9/9

    OOEDP Week 2 Lecture 2 9January 12

    Summary

    In this lecture we have:

    Introduced Packages

    Discussed the location and naming of packages