Chapter 3

Working with Statics

IN THIS CHAPTER

check Adding static fields to a class

check Creating static methods

check Creating classes that can be instantiated

check Working with static initializers

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 meanings, most of which relate to the idea of being stationary or unchanging. 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.

As used by Java, however, the term static doesn’t mean unchanging. You can create a static field, for example, and then assign values to it as a program executes. Thus, the value of the ...

Get Java All-in-One For Dummies, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.