Methods—Defining Behavior

Although variables define the state of a class instance, methods define the associated be havior that controls that state and do the actual work that takes place in your programs. Methods support access specifiers and modifiers in a similar manner to fields. The details of method declarations and syntax were discussed previously in Chapter 4. This section extends that discussion to look at some special uses of methods.

Constructors

A constructor is a special method that is called when a class instance is created. A constructor provides a place for you to define any initialization code you want executed for a new class instance. A constructor is named the same as the class and its declaration looks like a typical method ...

Get Special Edition Using Java 2 Standard 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.