Packages

Packages (sometimes called namespaces) are used to give a separate identity (name) to a group of classes. (Note: Packages are primarily used in Java and Actionscript.) For example, you are writing a fishing program and a finance program. Both programs use a Bank class but for entirely different reasons. To allow you to use the name "class Bank" for both distinct objects, you can place the first class in the Fish(.Bank) package and the second in the Finance(.Bank) package.

Packages

A Package is just a way of making sure all the names you choose to use in your program don't "step on the toes" of names someone else might use in their program. This is especially a problem in large software systems where you are using "libraries" (sets of pre-written code).

In the end, Packages are just a shortcut for naming things... you could name all of your classes: jims_star, jims_bank, jims_this, jims_that... but that would be tedious. Instead, at the very top of a file, we place a package declaration (e.g., package Jim) and the computer (behind the scenes) renames everything for us.

Folders

Operationally, a package is represented in the file system as a folder to contain the appropriate code. Thus if you want to create a new package, the first thing you would do would be to create a new folder to place the code in. The folder MUST have the same name as the package.

The Default Package

By definition, all classes must be in a package. If you don't declare a package name (thus leaving it blank) the computer places you code in the "default" package. Often you will get warnings saying: "It is discouraged to use the default package". For any large(ish) program you should spend the extra minute and put the code in a package.

Syntax

	
	  package Game_Utilties
	  {
	     //
	     // a class declaration goes here and the class is
	     // considered to be in that package.  Remember to
	     // place this code in the appropriate "Game_Utilities"
	     // folder
	     // 
	  }
	
      

Back to Topics List