6.2 Static Variables and Static Methods

One Universe made up of all that is; and one God in it all, and one principle of Being, and one Law, the Reason, shared by all thinking creatures, and one Truth.

—MARCUS AURELIUS, reported in Bartlett’s Familiar Quotations, 10th ed. (1919)

A class can have variables that are static as well as methods that are static. Static variables and static methods belong to a class as a whole and not to an individual object. In this section, we show you how to define static variables and static methods.

Static Variables

We have already used static variables in one special case, namely, in the definition of named constant values such as the following:

public static ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.