Static Methods

Often it is useful to have methods perform some function that does not depend on the contents of any object. Such a method can be defined as a static method. The following project file contains the programs described below.

For example, the Math class provides static methods for performing common mathematical calculations, as demonstrated by Another example is the Character class, which provides static methods for testing and operating on characters and is demonstrated by Note that the pattern for calling a static method is:


Defining Static Methods demonstrates how to define static methods; contains a main method that tests them.

The MyMath class defines a class constant and three static methods that are intended to behave like their Math class counterparts. Note how the static keyword is used.