Creating Methods

As you learned on Day 4, "Working with Objects," methods define an object's behavior—anything that happens when the object is created and the various tasks the object can perform during its lifetime.

This section introduces method definition and how methods work. Tomorrow's lesson has more detail about advanced things you can do with methods.

Defining Methods

Method definitions have four basic parts:

  • The name of the method

  • A list of parameters

  • The type of object or primitive type returned by the method

  • The body of the method

The first two parts of the method definition form what's called the method's signature.

Note

To keep things simpler today, two optional parts of the method definition have been left out: a modifier, such as ...

Get Sams Teach Yourself Java 2 in 21 Days, Second 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.