Short on time? This page is the recap. For explanations and worked examples, use the full study guide.
Recap
- Class methods are associated with the class, not with instances of the class.
- Class methods include the keyword
staticin the header, before the method name. - Class methods are typically called using the class name and the dot operator:
Converter.toFahrenheit(100). - When the call occurs in the defining class, the use of the class name is optional.
- Everything about parameters, arguments, return types, void and non-void methods from Topic 1.9 still applies.
- The
Mathclass contains only class methods, so it is never instantiated.
Key relationships
| Situation | Form of the call |
|---|---|
| Calling from a different class | ClassName.method(args) |
| Calling from inside the defining class | method(args) or ClassName.method(args) |
| Library class method | Math.sqrt(16) |
| Void method | a statement: Converter.banner("x"); |
| Non-void method | an expression: double f = Converter.toFahrenheit(100); |
| Nested call | f(g(x)): g(x) first, then f with that value |
Argument is an int, parameter is a double |
allowed (converted) |
Argument is a double, parameter is an int |
compile-time error |
| Compile-time error | Typical cause |
|---|---|
| cannot find symbol | method defined in another class and the class name is missing, or a misspelt name |
| cannot be applied to given types | wrong number of arguments |
| incompatible types | wrong argument type, or a returned value stored in the wrong type |
| void cannot be converted | using the result of a void method |
Assumptions and conventions
staticis shown in headers of class methods; non-static (instance) methods come in Topic 1.14.- The class name is the name of the class that defines the method, not the name of a variable.
- Arguments are evaluated before the method is entered.
- No calculator is allowed on the exam; trace nested calls on paper from the inside out.
Class methods and instance methods
- Class method: needs no object;
staticin the header; called through the class name (Math.abs(-3)). Used for work that does not depend on any one object. - Instance method: belongs to an object; called on the object (
word.length()); you cannot call it with only the class name (Topics 1.12 to 1.14). mainis itself a class method, which is why it can start a program before any object exists.- Class names are case-sensitive:
Converteris notconverter.
Mistakes to avoid
- Calling a method from another class without its class name.
- Reading
staticas “constant”. - Creating an object just to call a class method (such as
new Math()). - Passing a
doubleto anintparameter. - Putting arguments in the wrong order.
- Storing the result of a void method.
- Working a nested call from the outside in.
Quick self-check
- What keyword marks a class method? (
static) - How do you call
wholeMinutesof classConverterfrom another class, with the argument 200? (Converter.wholeMinutes(200)) - Is the class name optional inside
Converteritself? (Yes.) - What does
Math.sqrt(Math.abs(-9))return? (3.0) - Why does
Converter.wholeMinutes(2.5)not compile? (The parameter is anintand adoubleargument cannot be passed to it.)
Next: practice questions.