Você está na página 1de 5

PBO, wagito@akakom.ac.id, wagito@yahoo.

com --------------------------------------------------- --------------------------------------------- --------------------------How to Create a Java Package When using any object oriented language like Java the rogrammer will create classes - lots o! them. " rogrammer can organi#e these classes by introducing Java ackages. Java is an object oriented rogramming language and any Java rogrammer will $uickly build a large number o! di!!erent classes !or use in each a lication that they develo . %nitially it may be tem ting to store the classes in the same directory as the end a lication, but as the com le&ity a lications grow so will the number o! classes. 'he answer is to organi#e the classes into packages. The Java Package " Java ackage is sim ly a directory that contains one or more class !iles. %t is, there!ore, just a matter o!( creating a !older moving the class !ile to the new !older )or creating them there* calling the ackage !rom any a lications O! course, nothing is ever quite that sim le. Creating a Java Package 'he !irst ste is )rather obviously* to create the directory !or the ackage. 'his can go in one o! two laces( in the same !older as the a lication that will be using it in a location listed in the system CLASSPATH environmental variable +o, !or e&am le, i! ,-"++P"'. contains C:\Java\Packages then the new ackage can be created there, !or e&am le C:\Java\Packages\suite101, and once the directory has been created then classes can be added to it. 'he !irst line o! a class in a ackage is essential - it must contain the name o! o! the ackage )i.e. the directory in which the class resides*( package suite101; /o code is allowed above the ackage statement, but a!ter that critical line the class can be de!ined as normal. 0or e&am le it can call any ackages that it re$uires( import java.util.* ; import java.text.* ; %n this e&am le the class contains a single method which returns today1s date !ormatted !or the current locale( public class formatted_date { public static String today ! { "ate mydate # ne$ "ate !; String dateString # "ate%ormat.get"ate&nstance !.format mydate!; return dateString; ' ' "s always the code must be saved to a te&t !ile with the same name as the class - in this case that1s C:\Java\Packages\suite101\formatted_date. ava. Using a Java Package Packages are loaded into new a lications by using the import method as shown in the above e&am le where the !ollowing line was used( import java.util.* ; 'his is a statement recogni#ed by many Java rogrammers and means 2load all o! the the classes stored in the the java.util ackage2. ,ustom ackages are loaded into a Java a lication e&actly the same way( import suite101.* ; 'his will load any classes !ound in the suite343 directory )the location o! which is de!ined by the ,-"++P"'. environmental variable*. 'he formatted_date class can now be used in the class !or the new a lication(

PBO, wagito@akakom.ac.id, wagito@yahoo.com --------------------------------------------------- --------------------------------------------- ---------------------------

public class use_packages { public static void main String args()! { formatted_date ne$day # ne$ formatted_date !; System.out.println ne$day.today !!; ' ' 'he a lication code must be saved to a !ile with the same name as its class, but this !ile may be laced in any location on the com uter )!or e&am le C:\Java\Apps\creati!g_packages\use_packages. ava* and this can be com iled and run as re$uired. +ummary Java ackages are used to grou and organi#e classes( a ackages is a !older )or directory* on a com uter the location o! ackages are de!ined by using the ,-"++P"'. environmental variable the ackage may contain one or more classes each class in the ackage must have the !ollowing as its !irst line( package *package name+;

'he classes in the ackage are loaded into a Java a import *package name+.5 6

lication by using(

7iving the Java rogrammer the ability to create and to organi#e classes in the most e!!icient ways ossible.

PBO, wagito@akakom.ac.id, wagito@yahoo.com --------------------------------------------------- --------------------------------------------- --------------------------Pemberian nama aket selalu menggunakan huru! kecil contoh membuat aket geometri - dibuat direktori geometri di bawah direktori kerja - buat kelas +egiem at, -ingkaran dan lain-lain - ada bagian aling atas masing-masing kelas diberi tambahan erintah package geometri; - ada kelas 8tama diberi erintah import geometri.* - kom ilasi dan jalankan file, -geometri-Segiempat.java package geometri; public class Segiempat { private double pj; private double lb; public double luas ! { return pj*lb!; ' public Segiempat double p. double l! { pj#p; lb#l; ' ' file, -geometri-/ingkaran.java package geometri; public class /ingkaran { protected double jari; public /ingkaran ! { jari#0.0; ' public /ingkaran double r! { jari#r; ' public /ingkaran /ingkaran l! { jari#l.jari; ' public double luas ! { return 0.11*jari*jari!; ' ' file, 2tama.java import geometri.*; public class 2tama { public static void main String() args! { double a; Segiempat se#ne$ Segiempat 3.0. 4.0!; a#se.luas !; System.out.println 5luas Segiempat#56a!;

PBO, wagito@akakom.ac.id, wagito@yahoo.com --------------------------------------------------- --------------------------------------------- --------------------------/ingkaran li#ne$ /ingkaran 7.0!; a#li.luas !; System.out.println 5luas /ingkaran#56a!; ' '

PBO, wagito@akakom.ac.id, wagito@yahoo.com --------------------------------------------------- --------------------------------------------- --------------------------8ertanyaan, 9uatla: kelas /ingkaran lengkap dengan konstruktor. masukkan dalam paket geometri. bagaimana caranya. 9uatla: program 2tama untuk menggunakan kelas /ingkaran

Você também pode gostar