Chapter 15
Creating New Java Methods
IN THIS CHAPTER
Writing methods that work with existing values
Building methods that modify existing values
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access