Docsity

Packages - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Packages - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Packages

Introduction

  • Packages are nothing more than the way we organize files into different directories according to their functionality, usability as well as category they should belong to.
  • A Java package is a Java programming language mechanism for organizing classes into namespaces.

Introduction

Introduction

  • Packaging also help us to avoid class name collision when we use the same class name as that of others.
  • For example, if we have a class name called "Vector", its name would crash with the Vector class from JDK. However, this never happens because JDK uses java.util as a package name for the Vector class (java.util.Vector ).
  • Understanding the concept of a package will also help us manage and use files stored in jar files in more efficient ways.

Package access protection

  • Classes within a package can access classes and members declared with default access and class members declared with the protected access modifier.
  • Default access is enforced when neither the public, protected nor private access modifier is specified in the declaration.

Creation Of Jar Files

  • In Java source files the package the file belongs to is specified with the package keyword.
  • package java.awt.event;
  • JAR Files are created with the jar command-line utility.
  • The command “jar cf myPackage.jar *.class” compresses all *.class files into the JAR file myPackage.jar.

Organizational Package Naming Conventions

  • Package names should be all lowercase characters whenever possible.
  • Frequently a package name begins with the top level domain name of the organization and then the organization's domain and then any subdomains listed in reverse order.
  • The organization can then choose a specific name for their package..

Package Design Guidelines

  • Design Guideline Package Cohesion
    • Only closely related classes should belong to the same package.
    • Classes that change together should belong to the same package.
    • Classes that are not reused together should not belong to the same package.

Coding and Compiling

  • At the top of each of your source files (before any imports or anything else other than comments), you should have a package declaration. For example, CompanyApp.java would start with: package com.mycompanypackage;

Running the application

  • Many people "accidentally" end up with their classes in the right place, etc, just by luck, but then run into errors like:
  • java.lang.NoClassDefFoundError: MyCompanyApp (wrong name: com/mycompanypackage/MyCompanyApp.
  • c:\java\com\mycompanypackage> java MyCompanyApp

Package Declaration

  • Package declaration is file based;
    • All classes in the same source file belong to the same package.
    • Each source file may contain an optional package declaration in the following form. Package packagename;
    • Let us consider the source file ElevatorFrame.java, for example. Package elevator; Public class ElevatorFrame { public double x; //……..}

Package Declaration

  • The package declaration at the top of the source file declares that the ElevatorFrame class belongs to the package named elevator.
  • When the package declaration is absent from a file, all the classes contained in the file belong to unnamed package.
  • A class in a named package can be referred in two ways.

Importing a class in the package

  • Importing the class using the simple class name
    • We can import a class or all the classes in the designated package using Import packagename.ClassName; Import packagename.*;
    • The ElevatorPanel class in package elevator can simply be referred to as elevator when either of the following import clauses occurs at the top of source file Import elevator.ElevatorPanel; Import elevator.*;

Packages – Directory Paths

  • The CLASSPATH
    • List of directories and/or jar files. The compiler will look in these directories for any precompiled files it needs.
    • The CLASSPATH can be set as an environmental variable or specified on the command line using –classpath option.
    • The CLASSPATH is also used by the Java Virtual Machine to load classes.