Unformatted text preview:

PackagesIntroductionIntoductionSlide 4Slide 5Using PackagesPackage access protectionCreation Of Jar FilesPackage Naming ConventionsOrganizational Package Naming ConventionsPackage Design GuidelinesCoding and compilingCoding and CompilingRunning the applicationRunning the ApplicationPackage DeclarationSlide 17Using PackagesImporting a class in the packagePackages – Directory PathsCompile Package ClassesCommon MistakesIncorrect CLASSPATHSolutionReferencesPackagesSudhir Talasila Preeti NavaleIntroduction•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.Intoduction•Java source files belonging to the same category or providing similar functionality can include a package statement at the top of the file to designate the package for the classes the source file defines. •Java packages can be stored in compressed files called JAR files. •An obvious example of packaging is the JDK package from SUN (java.xxx.yyy) as shown below: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. IntroductionUsing Packages•To use a package inside a Java source file, it is convenient to import the classes from the package with an import statement.•import java.awt.event.*; •The above statement imports all classes from the java.awt.event package.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.Package Naming Conventions•Packages are usually defined using a hierarchical naming pattern, with levels in the hierarchy separated by periods (.) .•Although packages lower in the naming hierarchy are often referred to a "subpackages" of the corresponding packages higher in the hierarchy, there is no semantic relationship between packages.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 the 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;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 MyCompanyAppRunning the Application•Here's how to avoid it: •Stay in your "root" directory, eg c:\java •Always use the fully qualified classname. So, for example:• c:\java> java com.mycompanypackage.MyCompanyAppPackage 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.Using Packages–Class in a named package can be referred to in two different ways•Using the fully qualified name packagename.ClassName•We can refer to the ElevatorPanel class in package elevator as elevator.ElevatorPlanelImporting 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.Compile Package Classes•Compile the program–To compile the program, we must change the working directory to the source directory root, and issue the following command c:\project> javac -d . elevator\*.java –By compiling everything, we get the recent version of all the classes.–Specify –d . option to


View Full Document

NJIT CS 602 - Packages

Download Packages
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Packages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Packages 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?