Anda di halaman 1dari 6

Learning Outcomes

Pada akhir pertemuan ini, diharapkan


mahasiswa akan mampu:
– Menerapkan penggunaan package untuk
kelas-kelas di Java.
Package

Matakuliah : T0984 / Algoritma dan Metode Object Oriented Programming II


Pertemuan : 10
Tahun : 2008
Versi : 1/0 Bina Nusantara

Outline Materi Introduction

• Pengaturan file-file dalam satu direktori yang memiliki


fungsi yang sama.
• Introduction • Mempermudahkan kita dalam pengembangan sebuah
• Creating and Naming Package proyek yang besar.
• Contoh package JDK dari SUN (java.xxx.yyy) seperti
• Using package yang ditunjukkan di bawah ini:
• Sample Code

Bina Nusantara Bina Nusantara

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.

Bina Nusantara Bina Nusantara


http://arief.ismy.web.id

1
Example Example

//in the Draggable.java file


public interface Draggable {
...
}
Point
//in the Graphic.java file
public abstract class Graphic {
...
}
Also write an interface, Draggable, that
classes implement if they can be dragged //in the Circle.java file
public class Circle extends GraphicObject implements Draggable
with the mouse. {
...
}

Bina Nusantara Bina Nusantara


http://arief.ismy.web.id http://arief.ismy.web.id

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.

Bina Nusantara Bina Nusantara


http://arief.ismy.web.id http://arief.ismy.web.id

Creating a Package Example


//in the Draggable.java file
• Choose a name for the package package graphics;
public interface Draggable {
• Put a package statement with that name at ...
the top of every source file that contains the }
types (classes, interfaces) that you want to //in the Graphic.java file
include in the package. package graphics;
public abstract class Graphic {
• The package statement (for example, ...
}
package graphics;) must be the first line in
the source file. //in the Circle.java file
package graphics;
• There can be only one package statement in public class Circle extends GraphicObject implements Draggable {
...
each source file, and it applies to all types in }
the file.

Bina Nusantara Bina Nusantara


http://arief.ismy.web.id http://arief.ismy.web.id

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.

//in the Point.java file Contoh : D:\Java\Proyek\MyPackage\Poligon


package graphics;
public class Point extends GraphicObject implements Draggable {
... 2. Buat kelas (atau interface) yang akan menjadi isi
} package yang akan kita buat dengan susunan:
// Deklarasi package
//in the Line.java file package namapaket;
package graphics; // Deklarasi kelas
public class Line extends GraphicObject implements Draggable { public class namakelas {

... }
}

Bina Nusantara Bina Nusantara


http://arief.ismy.web.id

Creating and Naming Package Creating and Naming Package


( Lanjut ..) ( Lanjut ..)
3. Deklarasi Package 5. Setting CLASSPATH
Menggunakan kata kunci package yang ditulis di Atur variabel lingkungan CLASSPATH sehingga menunjuk ke
baris pertama pada file sumber (.java). directory tempat dimana package disimpan:
package namapaket; a. Melalui Control Panel
Contoh : b. Melalui perintah set path di command line
package MyPackage; Hanya boleh ada satu pernyataan package c. Melalui Configure – Preferences pada editor JAVA.
pada setiap file sumber.
package MyPackage.Poligon; SET CLASSPATH = .; D:\Java\Proyek;
Ketika file java (class atau interface) dikompilasi maka akan file .class
akan mengisi ke package yang ditunjuk.
4. Deklarasi Kelas
Dinyatakan secara public supaya bisa diakses oleh semua
kelas yang berada didalam dan diluar package yang dibuat.
Jika ada beberapa kelas pada file sumber, hanya boleh ada
satu kelas yang dinyatakan secara public, yaitu kelas yang
namanya sama dengan nama file sumber.

Bina Nusantara Bina Nusantara

Naming a Package Naming a Package

• 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

Using Package Members


Using Package

• 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);

Bina Nusantara Bina Nusantara


http://arief.ismy.web.id

Referring to a Package Member by Its Qualified Name Importing a Package Member


• use a package member's simple name (eg. • To import a specific member into the current
Rectangle) if the code you are writing is in the same file, put an import statement at the beginning
package as that member or if that member has been
imported.
of the file before any type definitions but after
the package statement, if there is one.
• to use a member from a different package and that
package has not been imported, you must use the
member's fully qualified name (eg. import graphics.Rectangle;
graphics.Rectangle)
• use this qualified name to create an instance of
….
graphics.Rectangle: Rectangle myRectangle = new
Rectangle();
graphics.Rectangle myRect = new graphics.Rectangle();

• works well if you use just a few members from


• Only for infrequent use. the graphics package.
Bina Nusantara Bina Nusantara
http://arief.ismy.web.id http://arief.ismy.web.id

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

Sample Code Sample Code ( lanjut .. )

• 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

Bina Nusantara Bina Nusantara

Name Ambiguities Managing Source and Class Files

• If a member in one package shares its name // in the Rectangle.java file


with a member in another package and both
package graphics;
packages are imported, you must refer to
each member by its qualified name. public class Rectangle() {
• If both graphics and java.awt have been ...
imported, the following is ambiguous. }
Rectangle rect;

• class name graphics.Rectangle


• use the member's fully qualified name
graphics.Rectangle rect; • pathname to file
graphics\Rectangle.java
Bina Nusantara Bina Nusantara
http://arief.ismy.web.id http://arief.ismy.web.id

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

Bina Nusantara Bina Nusantara


http://arief.ismy.web.id http://arief.ismy.web.id

Setting the CLASSPATH System Variable


Summary

• 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

Bina Nusantara Bina Nusantara


http://arief.ismy.web.id http://arief.ismy.web.id

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/

Bina Nusantara Bina Nusantara

Anda mungkin juga menyukai