advertisement: support javaworld, click here!
march 1999homefeatured tutorialscolumnsnews & reviewsforumjw resourcesabout jw
archive
topical index
core java
enterprise java
micro java
applied java
java community
java q&a index
java tips index
javaworld services
free javaworld newsletters
productfinder
education resources
white paper library
new! rational resources
design techniques
design with static members
how to put static fields and methods to work
summary
in this installment of his design techniques column, bill venners discusses the ways in which static fields and methods, which exist outside of objects, fit in a design that's object-oriented. (1,500 words)
by bill venners
printer-friendly version | mail this to a friend
page 1 of 2
advertisement
lthough java is object-oriented to a great extent, it is not a pure object-oriented language. one of the reasons java is not purely object-oriented is that not everything in it is an object. for example, java allows you to declare variables of primitive types (int, float, boolean, etc.) that aren't objects. and java has static fields and methods, which are independent and separate from objects. this article gives advice on how to use static fields and methods in a java program, while maintaining an object-oriented focus in your designs.
the lifetime of a class in a java virtual machine (jvm) has many similarities to the lifetime of an object. just as an object can have state, represented by the values of its instance variables, a class can have state, represented by the values of its class variables. just as the jvm sets instance variables to default initial values before executing initialization code, the jvm sets class variables to default initial values before executing initialization code. and like objects, classes can be garbage collected if they are no longer referenced by the running application.
nevertheless, significant differences exist between classes and objects. perhaps the most important difference is the way in which instance and class methods are invoked: instance methods are (for the most part) dynamically bound, but class methods are statically bound. (in three special cases, instance methods are not dynamically bound: invocation of private instance methods, invocation of init methods (constructors), and invocations with the super keyword. see resources for more information.)
another difference between classes and objects is the degree of data hiding granted by the private access levels. if an instance variable is declared private, only instance methods can access it. this enables you to ensure the integrity of the instance data and make objects thread-safe. the rest of the program cannot access those instance variables directly, but must go through the instance methods to manipulate the instance variables. in an effort to make a class behave like a well-designed object, you can make class variables private and define class methods that manipulate them. nevertheless, you don't get as good a guarantee of thread safety or even data integrity in this way, because a certain kind of code has a special privilege that gives them direct access to private class variables: instance methods, and even initializers of instance variables, can access those private class variables directly.
so the static fields and methods of classes, although similar in many ways to the instance fields and methods of objects, have significant differences that should affect the way you use them in designs.
treating classes as objects
as you design java programs, you will likely encounter many situations in which you feel the need for an object that acts in some ways like a class. you may, for example, want an object whose lifetime matches that of a class. or you may want an object that, like a class, restricts itself to a single instance in a given name space.
in design situations such as these, it can be tempting to create a class and use it like an object in order to define class variables, make them private, and define some public class methods that manipulate the class variables. like an object, such a class has state. like a well-designed object, the variables that define the state are private, and the outside world can only affect this state by invoking the class methods.