![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Part II: The sun ClassesPart II delves into the sun packages, a group of undocumented classes that add considerable power to Java programs. The java packages provide the public API that most programmers use, but the sun packages work behind the scenes. Many of Suns Java development tools, like javac and the appletviewer, are built from Sun classes. Furthermore, many of the public classes and interfaces in the JDK privately use the sun classes. The following are just a few of the capabilities hidden inside the sun classes:
As you can see, Sun has hidden a lot of functionality inside the sun packages. This book reveals it. Part II is dangerous because these classes may not be present in future releases of Java. They may not even be present in Java implementations not written by Sun. Even if they are present, their public methods may not have the same signatures. Classes that are public in one version may have only package access in the future. They may even move from one package to another. Nonetheless, these classes provide too much additional power to be ignored, and there are some very simple techniques provided here that enable you to use these packages safely in even non-conforming implementations. Part III: Platform-Dependent JavaPart III explores the possibilities opened by platform-dependent code. It demonstrates how to call the native API and how to create stand-alone executable programs that take advantage of unique abilities of the local platform. This part is dangerous because it limits the audience of a program. Its also dangerous because it violates many of the security restrictions normally imposed on Java programs. Nonetheless, not all programs are applets on Web pages. Many programs can benefit from taking advantage of native code, either for speed or to add additional functionality not present in the AWT. There are ways to use platform-dependent code to enhance your application without making your program inaccessible to users on all other platforms. This section explores these possibilities. Why Java Secrets?Relying on implementation-specific details opens up the possibility that your programs may stop working when Sun revises Java. Using the sun packages means that not all Java environments may be able to run your programs. Using native code limits your audience, increases the time-to-market, and makes your program buggier. There are ways you can limit these bad effects, but they are real, and they must be considered. Given these problems, why would anyone want to learn about the material in this book? I can think of several reasons. Broader applicabilityIn some cases, the design of Java limits you to a very small portion of the programs you might want to write. For example, the getAudioClip() and loop() methods of the Applet and AppletContext classes let your applets play sounds. Only applets can play sounds, however. In fact, its a little worse than that. Only applets that run in a Web browser or an applet viewer can play sounds. Applet subclasses that you instantiate in main() or embed in your own programs cannot play sounds because they dont have an AppletContext.
More power
InspirationThe sun classes are often a fertile source of ideas. Although many of the classes and packages are incomplete, they often provide a pattern on which you can model your own, more functional classes.
Sometimes you can create your own classes by extending Suns; some-times youll copy and paste; sometimes youll write your own classes from scratch using a similar API. Whichever you choose, its almost always easier to start with a good design and correct some minor deficiencies than it is to design a class from scratch. Of course, not all of Suns designs are good. Sometimes you can learn from the mistakes made in the sun classes so you dont repeat them. Not every class described in this book actually performs as advertised, and Ill be sure to tell you when thats so. One reason that some classes and packages are undocumented is that theyre buggy, poorly designed, or incomplete. Learning from your mistakes is good. Learning from someone elses mistakes is even better. You can learn from Suns mistakes. Where Did the Secrets Come From?Some people have asked how I discovered this information. To be perfectly honest, it really wasnt that hard. Sun has not gone to particularly great lengths to hide Javas internal structure from nosy eyes.
|
![]() |
|