Saturday, December 13, 2008

Statics

Working with Statics

Astatic method is a method that isn't associated with an instance of a class. (Unless you jumped straight to this chapter, you already knew that.) Instead, the method belongs to the class itself. As a result, you can call the method without first creating a class instance. In this chapter, you find out everything you need to know about creating and using static fields and methods.

Understanding Static Fields and Methods

According to my handy Webster's dictionary, the word static has several different meanings. Most of them relate to the idea of being stationary or unchanging. For example, a static display is a display that doesn't move. Static electricity is an electrical charge that doesn't flow. A static design is a design that doesn't change.

The term static as used by Java doesn't mean unchanging. For example, you can create a static field, and then assign values to it as a program executes. Thus the value of the static field can change.

To further confuse things, the word static can also mean interference, as in radio static that prevents you from hearing music clearly on the radio. But in Java, the term static doesn't have anything to do with interference or bad reception.

So what does the term static mean in Java? It's used to describe a special type of field or method that isn't associated with a particular instance of a class. Instead, static fields and methods are associated with the class itself. That means you don't have to create an instance of the class to access a static field or methods. Instead, you access a static field or method by specifying the class name, not a variable that references an object.

Static fields and methods have many common uses. Here are but a few:

  • To provide constants or other values that aren't related to class instances. For example, a Billing class might have a constant named SALES_TAX_RATE that provides the state sales tax rate.

  • To keep a count of how many instances of a class have been created. For example, a Ball class used in a game might have a static field that counts how many balls currently exist. This count doesn't belong to any one instance of the Ball class.

  • In a business application, to keep track of a reference or serial number that's assigned to each new object instance. For example, an Invoice class might maintain a static field that holds the invoice number that is assigned to the next Invoice object created from the class.

  • To provide an alternative way to create instances of the class. An excellent example of this is the NumberFormat class, which has static methods such as getCurrencyInstance and getNumberInstance that return object instances to format numbers in specific ways. One reason you might want to use this technique is to create classes that can have only one object instance. This is called a singleton class, and is described more in the sidebar "The Singleton pattern," which appears later in this chapter.

  • To provide utility functions that aren't associated with an object at all. A good example in the Java API library is the Math class, which provides a bunch of static methods to do math calculations. An example you might code yourself would be a DataValidation class with static methods that validate input data, or a database class with static methods that perform database operations.

No comments:

Post a Comment