The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.

Object and Class Methods

There are two types of methods in Java, called class methods and object methods. So far, every method we have written has been a class method. Class methods are identified by the keyword static in the first line. Any method that does not have the keyword static is an object method.

Although we have not written any object methods, we have invoked some. Whenever you invoke a method "on" an object, it's an object method. For example, drawOval is an object method we invoked on g, which is a Graphics object. Also, the methods we invoked on Strings in Chapter 7 were object methods.

Anything that can be written as a class method can also be written as an object method, and vice versa. Sometimes it is just more natural to use one or the other. For reasons that will be clear soon, object methods are often shorter than the corresponding class methods.

Last Update: 2011-01-24