Chapter 19

Creating New Java Methods

IN THIS CHAPTER

check Writing methods that work with existing values

check Building methods that modify existing values

check Making methods that return new values

In Chapters 3 and 4, I introduce Java methods. I show you how to create a main method and how to call the System.out.println method. Between that chapter and this one, I make very little noise about methods. In Chapter 18, I introduce a bunch of new methods for you to call, but that’s only half of the story.

This chapter completes the circle. In this chapter, you create your own Java methods — not the tired old main method that you’ve been using all along, but rather some new, powerful Java methods.

Defining a Method within a Class

In Chapter 18, Figure 18-6 introduces an interesting notion — a notion that’s at the core of object-oriented programming. Each Java string has its own equals method. That is, each string has, built within it, the functionality to compare itself to other strings. That’s an important point. When you do object-oriented programming, you bundle data and functionality into a lump called a class. Just remember Barry’s immortal words from Chapter 17:

A class … describes the way ...

Get Beginning Programming with Java For Dummies, 5th 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.