|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 3
Aside from acting as utility methods, class methods can be used to control access to objects stored in class variables --
in particular, to control how the objects are created or managed. Two examples of this kind of class method are the setSecurityManager() and getSecurityManager() methods of class System. The security manager for an application is an object that, like the standard input, output, and error streams, is needed
in many different places. Unlike the standard I/O stream objects, however, a reference to the security manager is not stored
in a public final class variable. The security manager object is stored in a private class variable, and the set and get methods
implement a special access policy for the object.
Java's security model places a special restriction on the security manager. Prior to Java 2 (previously known as JDK 1.2),
an application began its life with no security manager (getSecurityManager() returned null). The first call to setSecurityManager() established the security manager, which thereafter was not allowed to change. Any subsequent calls to setSecurityManager() would yield a security exception. In Java 2, the application always starts out with a security manager, but similar to the
previous versions, the setSecurityManager() method will allow you to change the security manager one time, at the most.
The security manager provides a good example of how class methods can be used in conjunction with private class variables to implement a special access policy for objects referenced by the class variables. Aside from utility methods, think of class methods as the means to establish special access policies for object references and data stored in class variables.
The main point of advice given in this article is:
Don't treat classes like objects.
If you need an object, make an object. Restrict your use of class variables and methods to defining utility methods and implementing special kinds of access policies for objects and primitive types stored in class variables. Although not a pure object-oriented language, Java is nevertheless object-oriented to a great extent, and your designs should reflect that. Think objects.
Next month's Design Techniques article will be the last of this column. I'll soon begin writing a book based on the Design Techniques material, Flexible Java, and will place that material on my web site as I go. So please follow that project along and send me feedback. After a break of a month or two, I'll be back at JavaWorld and SunWorld with a new column focused on Jini.
I encourage your comments, criticisms, suggestions, flames -- all kinds of feedback -- about the material presented in this column. If you disagree with something, or have something to add, please let me know.
You can participate in a discussion forum devoted to this material, enter a comment via the form at the bottom of the article, or e-mail me directly using the link provided in my bio below.