Java PackagesRef:
http://java.sun.com/docs/books/tutorial/java/packageYou can assemble classes into packages, bundling related classes into one unit (one name space).
Creating a Package e.g. a package named MyPackage
- create a directory called MyPackage/
- all classes intended for the package go in the directory
- First line of the java file for all classes in package: package MyPackage;
Using a Package - Within the package, you use the usual simple names e.g. just Class A.
- Outside of the package we must use qualified names: e.g. MyPackage.Class A or import members of the package then use simple names.
To import, the first line of the program would be:
import MyPackage.Class A;
import MyPackage.Class B;
or
import MyPackage.*;
Then use simple names.
Arguments vs. ParametersParameter: the variable input by a method.
e.g. public static double areaOfCircle (double
radius)
double y = 0;
double x = 1;
y = MyUtility.areaOfCircle(x);
The value of x should be unchanged after this operation. This is known as "passing by value". Within the method, a local copy of x is made.
That's the end of the story for non-object types.
The primitive types are not objects.Object types:Variable name (the value of the variable) is the location in memory where the object is stored: i.e. a pointer in some sense.
See Rectangle API in document e.g. Rectangle f = new Rectangle (3,4);
Knowing f gives access to f.width and f.height (via the Rectangle API);
"Passing by value" - a method is not allowed to change the value of the parameter in the calling method. Therefore a method cannot change the address of an object parameter.
A method, however, can change the contents of that memory address (if allowed by that API).
e.g. public static void twice(Rectangle x)
//intended to double width and height
Rectangle r = new Rectangle (3,4);
twice(r); //twice is allowed to change the value of r.width and r.height via the API.
//twice is not allowed to substitute r for a different object.