Download Package - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Software Engineering
Module: Core of Java
Topic: Packages
TALENTSPRINT | © Copyright 2012
Packages
The content in this presentation is aimed at teaching
learners to:
• Create a Package
• Work with predefined and user defined packages
TALENTSPRINT | © Copyright 2012
2
Packages
What is a Package?
A package is a grouping of related types (classes, interfaces,
Exceptions and Annotations) providing access protection and
Name Space Management.
Built-In packages are library packages.
Example: java.lang, java.awt etc.
Built-In
Package
User-defined packages are user developed
packages.
User-Defines
TALENTSPRINT | © Copyright 2012
3
Packages
Why Packages?
Using packages programmers can easily determine that
these types are related.
As packages follow naming conventions, a programmer
knows that all graphical related methods will be present in
a package named graphics.
The names of your types won't conflict with the type
names in other packages because the package creates a
new namespace.
You can allow types within the package to have
unrestricted access to one another yet still restrict access
for types outside the package.
TALENTSPRINT | © Copyright 2012
4
Packages
Creating a Packages?
Syntax to create a package:
package packagename;
Here package is the keyword used to create a package
followed by packagename.
Syntax to create a sub-package:
package package1.package2;
Here package1 is a package, which contains a subpackage
named package2.
Note
The package statement must be the first line in the source file.
TALENTSPRINT | © Copyright 2012
5
Packages
Program to demonstrate packages:
package world;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
How to compile the above program:
 javac -d . HelloWorld.java
 “-d” stands for “directory“ which explains the compiler
the location where the class files should be created.
 .(dot) stands for the current directory.
TALENTSPRINT | © Copyright 2012
6
Packages
What happens when we use package statement?
When we use a package statement, the underlying
operating system will create a directory with the same
name of the package.
As a programmer we should make sure that the .class
files should be compiled to the same package directory.
Make sure that you set the CLASSPATH to the directory
where the .class files are located.
After setting the classpath, we can run our programs as
follows:
>java packagename.classname
TALENTSPRINT | © Copyright 2012
7
Packages
Using Packages ?
Import statement is used to get access to the classes present
in packages.
Importing a Single Package Member:
Syntax: import packagename.Classname;
While importing the packages we need to mention the fully
qualified package name as above.
Example: import java.lang.Integer;
TALENTSPRINT | © Copyright 2012
8
Packages
Importing an Entire Package:
It can become a pain to type the fully qualified
everytime if we are using number of classes within
code may endup with a large number of import
avoid this we can import multiple classes with a
statement as follows:
package name
a package. The
statements. To
single package
Syntax: import packagename.*;
Asterisk(*) represents all the classes present in that package.
While importing the packages we need to mention the fully
qualified package name as above.
Example: import java.lang.*;
TALENTSPRINT | © Copyright 2012
9
Packages
TALENTSPRINT | © Copyright 2012
10