Chapter 8
Adding Some Methods to Your Madness
IN THIS CHAPTER
Seeing some good reasons to use methods in your programs
Creating methods that return values
Creating methods that accept parameters
In Java, a method is a block of statements that has a name and can be executed by calling (also called invoking) it from some other place in your program.
In this chapter, you find out how to create additional methods that are part of your application’s class. Then you can call these methods from your main method. As you’ll see, this technique turns out to be very useful for all but the shortest Java programs.
The Basics of Making Methods
All methods — including the main method — must begin with a method declaration. Here’s the basic form of a method declaration, at least for the types of methods we talk about in this chapter:
public static return-type method-name (parameter-list){ statements…}
The following paragraphs describe the method declaration piece by piece:
public: This keyword indicates that the method’s existence should be publicized to the world and that any Java program that knows about your program (or, more accurately, the class defined for your Java program) should ...
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