Friday, October 10, 2008

Using Package Members

This is an important lesson. Notwithstanding my comments on the linguistic style of the previous few lessons, java packages are central to the java model for sharing code.

Microsoft uses what they call dynamic link libraries (dll's) to store code shared by many applications. When you, the ordinary programmer, create an application which uses an object defined in a dll, VB, or whatever other programming application is being used, makes a note of it, and when you distribute the application, that, and any other dll you have used, gets included in the cab files. In contrast with the Java model, your own code is stored (after compiling) in an executable, and is not readily usable by other programmers or applications.

In the java model, it seems that even the most humble programmer, has the option of making the classes, which they create, usable by others. And because they have the option of doing so, they are encouraged to organise their work in the same manner which is required of paid programmers working in large organizations. Indeed they are more than encouraged. If they don't follow the rules, even for a very simple application, they are likely to come unstuck, as I discovered to my cost.

In a later blog, I shall describe in detail the fun I had with NetBeans, but for now all I need to say is that to make my code work, I had to create a custom class, and because I did not read this lesson properly, I attached my class to the wrong package, failed to import it properly, and nothing did work.

So the first rule of thumb is that if you are writing an application comprising objects from more than one class, attach all the class files to the same package. Now obviously if you want a graphic interface, you will apply code written by others, and which will belong to other packages. But if you are using a tool like NetBeans, a lot of the thinking on that side of it will be done for you. Where you have to be careful, is when you start creating custom objects from custom classes, because you have to assign everything you create to a package, and it pays to have done some thinking in advance as to how you are going to do this.

The "Hello World!" application sidesteps all these issues, because it doesn't call on any external code. It is entirely self contained. So it doesn't need to be assigned to a package. It therefore represents a very bad template from which to learn Java.

The BicycleDemo is more misleading still, because it does create objects from a class, and neither it nor the class from which the objects are created appear to have been assigned to a package.

In practice, it seems, you need to use packages, and if you create an object from a class, if the class does not reside in the package in which you are writing, you need to refer to the class by its qualified name (as defined in the lesson), or you need to import either the class or the whole package (again as described in the lesson).

No comments: