phone: 016-6933793
e-mail: darknessofsorrow@hotmail.com

Wednesday 17 October 2012

Packages

A packages allows a developer  to group classes (and interfaces) together. These classes will all be related in some way - they might all be to do with a specific application or perform a specific set of tasks.
For example, the Java API is full of package. One of them is the javax.xml package. It and its subpackages contain all the classes in Java API to do with handing XML.







Defining a Package 
To group classes into a package each class must have a package statement defined at the top of its .java file. It lets the compiler know which package the class belongs to and must be the first line of code.
For example :
imagine we are making a simple Battleships game. It makes sense to put all the classes needed in a package called battleship :

 package battleships
 
 class GameBoard{
 
 } 
Every class with the above package statement at the top will not be part of the Battleship package.

Typically packages are stored in a corresponding directory on the file system but it possible to store them in a database. The directory on the file system must have the same name as the package. It's where all the classes belonging to that package are stored.
For example :
if the battleships package contains the classes GameBoard, Ship, ClientGUI then there will be files called GameBoard.java, Ship.java and ClientGUI.java stored in a directory call battleships.


Packages and Importing
Each file can contain several classes, enums and imports. They are all part of the package declared at the beginning of the file. If package is not declared then the default empty package is used. Each type has then a path corresponding to the package name followed by the type name.
  // file my/pack/C.hx
    package my.pack;

    enum E {
    }

    class C {
    }
This file declares two types : my.pack.E and my.pack.C. It's possible to have several classes in the same file, but the type name must be unique in the whole application, so conflicts can appear if we are not using packages enough (this does mean that we have to use long packages names everywhere). 

When creating types using packages, we should create a nested folder/directory structure matching the  package name, and our files defining the type should be created in the innermost folder/directory. The name of the file should generally match the type we are creating. For example, to create the types E and C, in the package my.pack as shown above, our folder structure be my\pack and the files could be E.hx and C.hx in the folder pack. In general,  the name of the file is the one containing the definition of the main class.

The file extension for Haxe is .hx.

Each part of the path in package names must begin with a lower case letter and, like types, type names in packages must begin with an upper case letter. Hence My.Pack is an invalid package, as is my.Pack.Similarly, my.pack.e would not be a valid type name or import.

0 comments: