6.3 static Methods, static Fields 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. 5.6. To declare a method as static, place the keyword static before the return type in the method’s declaration. For any class imported into your program, you can call the class’s ...

Get Java How to Program, Early 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.