5.3 static Methods, static Variables and Class Math

Most methods execute in response to method calls on specific objects. However, sometimes a method performs a task that does not depend on an object. Such a method applies to the class in which it’s declared as a whole and is known as a static method or a class method. (In Section 10.10, you’ll see that interfaces also may contain static methods.)

Classes often contain convenient static methods to perform common tasks. For example, recall that we used class Math’s static method pow to raise a value to a power in Fig. 4.6. For any class imported into your program, you can call the class’s static methods by specifying the class’s name, followed by a dot (.) and the method name, as in


ClassName.methodName(arguments) ...

Get Java How To Program, Late Objects, 11th 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.