![]() |
Grinding Java |
IntroductionWhen I first started studying object oriented methodology I was very confused. It seemed to me as if I was missing something since the ideas were so simple yet they were all the rave of highly skilled programmer's. After a while I decided that OO is rather simple and started developing in C++. But pretty soon I found out that understanding the syntax and basic concepts was not good enough. I experienced the same sort of confusion when approaching Java beans. When I read the explanations in Sun's pages it seemed a little to simple; it is. In essence we have already developed some Java beans. What are beans?Java Beans were created by Sun to follow the success of OCX's (now renamed ActiveX) and VBX's (mostly obsolete). Beans are pluggable components used by visual development tools to allow a programmer to add functionality in a simple and visual way. All of Java's AWT 1.1 Components are beans! Since we derive from these components we have essentially created very crude and very basic beans. What are the basic requirements of a bean?
I also used VisualAge for Java/2 which is the first Java development tool I found which was actually on par with today's compilers (and many times better). I highly recommend downloading VisualAge. Object serializationEvery bean must be serializable: in order to be serializable a class must implement the Serializable interface which has no methods! The serializable interface is in fact only a tag stating that the object was designed with serialization in mind. A serialized object must have a default constructor and must mark every field it does not wish to save with the word transient. The ObjectInputStream class and ObjectOutputStream class do all the work of converting the objects to streams. If you wish to have control over the way your object converts to a stream check out the Externalizable interface. The code in the experiments directory has the following output: A string representing the start of the file. a = 5 b = 6 c = 0 a = 11 b = 12 c = 0a is public and c is private, c is transient so it is never saved. Object serialization is used by developers to save the state of the bean. JAR filesJohn Dvorak called Java applets Crapplets referring to the slow download time of Java applets. To download an applet you need to open several TCP connections and download small but uncompressed class files. JAR came to change all of that. The JAR file format groups both the class files and all the resources into a single file with optional ZIP compression. In order to read from a Jar file we must used the package java.util.zip which contains several classes of interest. In Java reading from a ZIP file is no more complex than from a disk file. The naming conventionJava beans have a very simple yet powerful naming convention. In fact, one of the biggest changes in JDK's 1.1 AWT was in making the AWT Components consistent with the Java beans naming conventions. The two most important conventions are:
There's moreJava beans allow lots of other interesting tricks such as a changing the behavior of beans during design time, and conventions regarding property changes. These can all be found at the Java beans home page at Sun. Sun will soon release a new Java bean spec codenamed Glasgo which promises some interesting extensions. The visual developerAs I promised the previous month I made some dramatic improvements and fixes to the visual developer and the FreeFormEdit class. Since I started working from the existing code it will be very hard to document all of my changes so instead I will try to describe the overall changes. I used the JavaDoc utility to document my code for easier browsing. JavaDoc and source zip file.
Room for improvement
SummaryThis column has been shorter than usual since I did not have time to add some features I wanted to add. Next month I will delve into security and some of the inside guts of the virtual machine. |