Package
Introduction ( lanjut .. )
• A package is a grouping of related types providing
access protection and name space management. • Manfaat menggunakan package :
Note that types refers to classes, interfaces etc.
– Menghindari tabrakan nama kelas yang kita buat dengan kelas
• Organize a set of related classes and interfaces. yang sudah ada.
• Similar to different folders on your computer. – Mudah dalam pemeliharaan dan pengembangannya
• You might keep HTML pages in one folder, images (pengelompokan).
in another, and scripts or applications in yet – Memudahkan dalam mencari dan menggunakan nama kelas
another. dan mengkontrol aksesnya.
• Because software written in the Java programming – Memahami konsep dari package akan membantu dalam
language can be composed of hundreds or mengelola dan menggunakan file yang disimpan dalam JAR (
Java ARchieve) lebih efisien.
thousands of individual classes, it makes sense to
keep things organized by placing related classes
and interfaces into packages.
1
Example Example
Example Example
//in the Rectangle.java file You should bundle these classes and the interface in a
public class Rectangle extends GraphicObject implements package for several reasons, including the following:
Draggable {
...
} • You and other programmers can easily determine
that these types are related.
//in the Point.java file • You and other programmers know where to find
public class Point extends GraphicObject implements Draggable { types that can provide graphics-related functions.
...
} • The names of your types won't conflict with the type
names in other packages because the package
//in the Line.java file creates a new namespace.
public class Line extends GraphicObject implements Draggable { • You can allow types within the package to have
... unrestricted access to one another yet still restrict
} access for types outside the package.
2
Example
Creating and Naming Package
//in the Rectangle.java file
package graphics;
public class Rectangle extends GraphicObject implements • Langkah membuat package :
Draggable {
... 1. Buat directory yang merepresentasikan tempat
} package yang akan dibuat.
• many programmers will use the same name • This works well unless two independent
for different types. programmers use the same name for
• Rectangle class is a class in the java.awt their packages.
package
• What prevents this problem?
• the compiler allows classes to have the same
name if they are in different packages. • Naming Convention.
• Rectangle class is a class in the graphics
package
• The fully qualified name of each Rectangle
class includes the package name:
graphics.Rectangle AND java.awt.Rectangle
Bina Nusantara Bina Nusantara
http://arief.ismy.web.id http://arief.ismy.web.id
3
Naming Convention Exception in Naming a Package
• Package names are written in all lowercase • if the domain name contains a hyphen
• Companies use their reversed Internet or other special character, for example if
domain name to begin their package names the package name begins with a digit or
• com.example.orion for a package named other character that is illegal to use as
orion created by a programmer at the beginning of a Java name, or if the
example.com package name contains a reserved
• Name collisions that occur within a single Java keyword, such as "int". In this event,
company need to be handled by the suggested convention is to add an
convention within that company. underscore.
• including the region or the project name
after the company name (for example, Domain Name Package Name Prefix
com.company.region.package).
clipart-open.org org.clipart_open
• Packages in the Java language itself begin
with java. or javax. free.fonts.int int_.fonts.free
Bina Nusantara
http://arief.ismy.web.id
Bina Nusantara poetry.7days.com com._7days.poetry
http://arief.ismy.web.id
• Ada 2 cara menggunakan kelas yang ada di • The types that comprise a package are
dalam package : known as the package members.
1. Dengan mereferensi ke nama package dari kelasnya (fully qualified
name)
Contoh :
Hasil kompilasi kelas SegiEmpat yang ada dalam direktori Poligon • To use a public package member from
MyPackage.Poligon.SegiEmpat S;
S = new MyPackage.Poligon.SegiEmpat(17, 8);
outside its package, you must do one
atau of the following:
MyPackage.Poligon.SegiEmpat S = new MyPackage.Poligon.SegiEmpat(17, 8);
– Refer to the member by its fully qualified name
2. Dengan menggunakan import – Import the package member
import MyPackage.Poligon.*; – Import the member's entire package
import MyPackage.Poligon.SegiEmpat; Wildcard semua class java yang ada
di direktori Poligon di import semua
Untuk penggunaannya :
SegiEmpat s = new SegiEmpat(17, 8);
4
Importing an Entire Package Using Package ( lanjut .. )
• To import all the types contained in a
particular package, use the import
statement with the asterisk (*) wildcard
character.
import graphics.*;
….
Circle myCircle = new Circle();
Rectangle myRectangle = new
Rectangle();
import graphics.A*; //does not work
Bina Nusantara Bina Nusantara
http://arief.ismy.web.id
• Contoh 2 file Java dalam package Poligon : • Jika digunakan maka : Untuk menggunakannya di-import dahulu kelas
yang akan dipakai
Memerintahkan hasil kompilasi kelas file Java yang dibuat akan diletakkan di dalam folder tersebut.
Jika folder belum dibuat maka kompiler akan membuatnya.
• Outputnya :
File Kelas SegiEmpat.java File Kelas SegiTiga.java
5
Managing Source and Class Files Classpath
// in the Rectangle.java file You can arrange your source and class
package com.example.graphics; directories separately, as:
public class Rectangle{ <path_one>\sources\com\example\graphics\
... Rectangle.java
}
<path_two>\classes\com\example\graphics\
Rectangle.class
• ....\com\example\graphics\Rectangle.java
• ....\com\example\graphics\Rectangle.class <path_two>\classes Î CLASSPATH variable
• Package:
To set the CLASSPATH variable – To make types easier to find and use
– To avoid naming conflicts
– To control access
C:\> set CLASSPATH=C:\users\arief\java\classes
• To create a package for a type, put a
package statement as the first statement in
To delete the current contents of the the source file that contains the type (class,
interface)
CLASSPATH variable • To use a public type that's in a different
C:\> set CLASSPATH= package, you have three choices:
(1) use the fully qualified name of the type,
(2) import the type
(3) import the entire package of which the type is a member.
To display the current CLASSPATH variable
C:\> set CLASSPATH
Summary
Referensi
Langkah membuat Package:
1. Buat directory yang merepresentasikan tempat package yang • Introduction to Java Programming. 7ed. Liang. 2009.
akan dibuat.
Contoh : D:\Java\Proyek\MyPackage\Poligon Chapter 7, hal 273
2. Buat kelas (atau interface) yang akan menjadi isi package • Package
yang akan kita buat 9 http://en.wikipedia.org/wiki/Java_package
3. Deklarasi Package
Menggunakan kata kunci package yang ditulis di baris 9 http://www.jarticles.com/package/package_eng.html
pertama pada file sumber (.java).
9 http://java.sun.com/docs/books/tutorial/java/package/packages.h
4. Deklarasi Kelas
Dinyatakan secara public supaya bisa diakses oleh semua kelas yang tml
berada didalam dan diluar package yang dibuat.
9 http://tinf2.vub.ac.be/~dvermeir/java/other_doc/JavaPackages.ht
5. Setting CLASSPATH
Atur variabel lingkungan CLASSPATH sehingga menunjuk ke directory
ml
tempat dimana package disimpan 9 http://dancpm.wordpress.com/2008/01/14/package-on-java-
programming/