Chapter 15

Creating New Java Methods

IN THIS CHAPTER

Bullet Writing methods that work with existing values

Bullet Building methods that modify existing values

Bullet Making methods that return new values

In Chapters 4 and 5, I introduce Java methods. I show you how to create a main method, how to call the System.out.println method, and how to use the Scanner class's nextLine method. Between Chapters 5 and 14, I make very little noise about methods. In Chapter 14, I introduce a bunch of new methods for you to call, but that's only half 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 14, Figure 14-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 ...

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